firebase - 用户注册后获取用户UID

标签 firebase flutter dart google-cloud-firestore firebase-authentication

用户注册后如何获取用户UID。我正在尝试使用getCurrentUser方法,但最终总是为null。这是方法:

  String userId;

  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  getCurrentUser() async {
    FirebaseUser firebaseUser = await FirebaseAuth.instance.currentUser();
    setState(() {
      userId = firebaseUser.uid;
    });
  }
这是用户要注册时的按钮,用户输入的数据将发送到Firebase。但是userId始终为null。
Container(
                  height: 45.0,
                  width: 270.0,
                  child: RaisedButton(
                    child: Text('SIGN UP'),
                    onPressed: () async {
                      setState(() {
                        showSpinner = true;
                      });
                      try {
                        final newUser =
                            await _auth.createUserWithEmailAndPassword(
                                email: email, password: password);
                        _firestore.collection('UserAccount').add({
                          'uid': userId,
                          'Email Address': email,
                          'Full Name': nama,
                          'Phone Number': phoneNumber,
                        });
                        if (newUser != null) {
                          Navigator.pushNamed(context, HomePage.id);
                        }
                        setState(() {
                          showSpinner = false;
                        });
                      } catch (e) {
                        print(e);
                      }
                    },
                  ),
                ),
如果我尝试使用上面的代码注册,则会出现此错误。
enter image description here
谢谢任何想要帮助的人:(

最佳答案

注册后需要获取uid:

 final newUser =
   await _auth.createUserWithEmailAndPassword(
   email: email, password: password);
 _firestore.collection('UserAccount').add({
                          'uid': newUser.user.uid,
                          'Email Address': email,
                          'Full Name': nama,
                          'Phone Number': phoneNumber,
                        });
newUser应该返回AuthResult类型的值,并且在AuthResult内它具有 FirebaseUser 类型的字段,因此您可以使用newUser.user.uid

如果要基于用户标识从文档中获取数据,则建议执行以下操作:
 final newUser =
   await _auth.createUserWithEmailAndPassword(
   email: email, password: password);
 _firestore.collection('UserAccount').document(newUser.user.uid).setData({
                          'Email Address': email,
                          'Full Name': nama,
                          'Phone Number': phoneNumber,
                        });

关于firebase - 用户注册后获取用户UID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62919410/

相关文章:

Firebase 在 M1 Mac 上损坏

javascript - firebase.auth 不是函数 - 具有多个 firebase 应用程序初始化

javascript - 我正在尝试从函数内提取 doc.data 数组并在范围之外使用它

flutter - 如何添加外部包以使用 visual studio 代码 flutter

java - 我在 Release模式下运行 Flutter 应用程序时遇到问题 - android

ios - 关闭应用程序时, bool 值未设置为否

firebase - 是否可以在 Dart 中定义一个抽象的命名构造函数?

android-studio - Flutter:任务 ':app:transformClassesAndResourcesWithProguardForRelease' 执行失败

dart - Flutter Drawer 作为单独的小部件不允许修改宽度

javascript - Dart2Js 但可读