firebase 无法使用静态访问来访问示例成员“user

bzzcjhmw  于 2023-03-13  发布在  其他
关注(0)|答案(1)|浏览(168)

我试图创建一个注册函数,但我得到这个错误“示例成员'用户'不能使用静态访问。”为此行
字符串userId =用户凭证.用户!.uid;
I am uploading error page here
这是密码

final auth = FirebaseAuth.instance;
final db = FirebaseFirestore.instance;
Future<bool> register(String email, String password, String username) async {
  try {
    await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password);

    
   String userId = UserCredential.user!.uid;
   final userRef = FirebaseFirestore.instance.collection('users').doc(userId);
    await userRef.set({
      'username': username,
      'email': email,
    });
    return true;
  } on FirebaseAuthException catch (e) {
    if (e.code == 'weak-password') {
      print('Password provided is too weak');
      global.error = "Password provided is too weak";
    } else if (e.code == 'email-already-in-use') {
      global.error = "The account already exist for that email.";
      print('The account already exist for that email.');
    }
    return false;
  } catch (e) {
    print(e.toString());
    return false;
  }
} ```
k97glaaz

k97glaaz1#

通过查看creating a user with email+passwordUserCredentials class文档中的代码示例,似乎获取新创建的用户及其UID的正确方法是:

var credentials = await FirebaseAuth.instance
        .createUserWithEmailAndPassword(email: email, password: password);
var user = credentials.user!;
var uid = user.uid;

相关问题