Flutter 和 firebase:未为“UserCredential”类型定义 getter“uid”

时间:2021-06-10 16:21:19

标签: firebase flutter dart firebase-authentication

我正在开发一个带有 flutter 和 firebase 的应用程序,我正在为用户进行身份验证,我希望你使用 uid 函数,但它不起作用,为什么?

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_database/firebase_database.dart';

class FirebaseHelper {

//Authentification
final auth = FirebaseAuth.instance;

Future<UserCredential> handleSignIn(String mail, String password) async{
 final UserCredential user = await auth.signInWithEmailAndPassword(email: mail, password: password);
 return user;
}

Future<UserCredential> handleCreate(String mail, String password, String prenom, String nom) async{
 final UserCredential user = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: mail, password: password);
 String uid = user.uid;
 Map<String, String> map = {
   "prenom" : prenom,
   "nom" : nom,
 };
 addUser(uid, map);
 return user;
}

//database

static final base = FirebaseDatabase.instance.reference();
// ignore: non_constant_identifier_names
final base_user = base.child("users");

addUser(String id, Map map){
 base_user.child(uid).set(map);
}

}

它是标记 The getter 'uid' isn't defined for the type 'UserCredential'.Undefined name 'uid'. 我不知道我能做什么...请帮帮我

1 个答案:

答案 0 :(得分:0)

在这种情况下,我发现将参考文档放在手边真的很有帮助。在那里,您可以看到 UserCredential 确实没有 uid 属性,它解释了错误消息。如果我们进一步浏览,您会注意到凭据具有 user 属性,而该属性又具有 uid

所以

 final UserCredential credential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: mail, password: password);
 String uid = credential.user.uid; // or credential.user?.uid if you're using null-safety