简体   繁体   English

无法从 firebase 中检索 DisplayName 名称和 PhotoUrl,始终返回 null,Flutter

[英]Unable to retrieve DisplayName name and PhotoUrl from firebase, always returning null, Flutter

I am trying to retrieve the displayName name photoUrl from firebase as i have created a user using google sign in and i want to use the display photo and display name as that of google account.我正在尝试从 firebase 检索 displayName 名称 photoUrl,因为我已经使用 google 登录创建了一个用户,并且我想使用显示照片和显示名称作为 google 帐户的名称。

child: ListTile(
              title: Text("${user.displayName}"),
              subtitle: Text("${user.username}"),
              leading: CachedNetworkImage(
                imageUrl: user.photoUrl,
                placeholder: (context, url) => circularProgress(),
              ),
            ),

在此处输入图片说明

在此处输入图片说明

Firebase Auth does not give you a guarantee that photoURL will contain a value. Firebase 身份验证不保证photoURL将包含一个值。 It can definitely be null, and you should check for that before you try to display it.它绝对可以为空,您应该在尝试显示它之前检查它。

So it was a noob mistake and i found a solution.所以这是一个菜鸟错误,我找到了解决方案。

It was a grammatical or more likely a case sensitive error.这是一个语法错误或更可能是区分大小写的错误。

Make sure that when you are setting the data for your storage, keep the name/spelling case same as when you were defining the pairs:确保在为存储设置数据时,保持名称/拼写与定义对时相同:

class User {
  final String username;
  final String photoUrl;
  final String id;
  final String email;
  final String displayName;
  final String bio;

  User(
      {this.username,
      this.photoUrl,
      this.id,
      this.email,
      this.displayName,
      this.bio});

  factory User.fromDocument(DocumentSnapshot doc) {
    return User(
      id: doc['id'],
      username: doc['username'],
      email: doc['email'],
      photoUrl: doc['photoUrl'],
      bio: doc['bio'],
      displayName: doc['displayName'],
    );
  }
}

Setting Value for Firebase: Firebase 的设置值:

 usersRef.document(user.id).setData({
        "id": user.id,
        "username": username,
        "photoUrl": user.photoUrl,
        "email": user.email,
        "displayName": user.displayName,
        "bio": "",
        "timestamp": timestamp
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM