【问题标题】:Flutter & firebase : The getter 'uid' isn't defined for the type 'UserCredential'Flutter & firebase:没有为“UserCredential”类型定义 getter 'uid'
【发布时间】:2021-08-27 16:32:29
【问题描述】:

我正在开发一个带有颤振和 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'. 我不知道我能做什么...请帮助我

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    在这种情况下,我发现将参考文档放在手边确实很有帮助。在那里,您可以看到 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
    

    【讨论】:

    • 感谢它可以正常工作,但我使用最终的UserCredential credential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: mail, password: password); String uid = credential.user!.uid;,现在它工作正常,但它在第 33 行对我说Undefined name 'uid'.(这里:addUser(String id, Map map){ base_user.child(uid).set(map);)}
    • 糟糕,? 或 ! 确实需要在 user 之后。如果这解决了原来的问题,那么新的错误就是一个新的问题,所以我建议发布一个带有更新代码和错误消息的新问题。
    • 我不能在 90 分钟之前发帖,但请发帖,谢谢
    猜你喜欢
    • 2021-05-21
    • 1970-01-01
    • 2021-10-04
    • 2020-09-02
    • 2021-01-25
    • 2021-10-19
    • 2021-05-18
    • 1970-01-01
    • 2021-11-22
    相关资源
    最近更新 更多