【问题标题】:Bad state: field does not exist within the DocumentSnapshotPlatform after updating my app错误状态:更新我的应用程序后 DocumentSnapshotPlatform 中不存在字段
【发布时间】:2021-08-29 16:48:05
【问题描述】:

更新我的颤振应用程序及其软件包后,我遇到了一个错误。错误状态:DocumentSnapshotPlatform 中不存在字段 这是给出错误的代码。

 FutureBuilder(
          future: usersRef.doc(widget.currentUserId).get(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            if (!snapshot.hasData) {
              return SizedBox.shrink();
            }
            AccountUser user = AccountUser.fromDoc(snapshot.data);

            return _locationTab(user);
          }),

更新后,我也面临与此代码相同的错误。

static Future<AccountHolder> getUserWithId(String userId) async {
DocumentSnapshot userDocSnapshot = await usersRef.doc(userId).get();
if (userDocSnapshot.exists) {
  return AccountHolder.fromDoc(userDocSnapshot.data());
}
return AccountHolder();

}

这是accountholder的代码,对不起,我把名字从accountUser改成了accountHolder。

import 'package:bars/widgets/exports.dart';

class AccountHolder {
  final String id;
  final String name;
  final String userName;
  final String profileImageUrl;
  final String email;
  final String bio;
 
 

  AccountHolder({
    this.id,
    this.name,
    this.userName,
    this.profileImageUrl,
    this.email,
    this.bio,
   
  });

  factory AccountHolder.fromDoc(DocumentSnapshot doc) {
    return AccountHolder(
      id: doc.id,
      name: doc['name'],
      userName: doc['userName'] ?? '',
      profileImageUrl: doc['profileImageUrl'],
      email: doc['email'],
      bio: doc['bio'] ?? '',
    );
  }
}


 

【问题讨论】:

    标签: firebase flutter firebase-realtime-database google-cloud-firestore


    【解决方案1】:

    改变这个:

                AccountUser user = AccountUser.fromDoc(snapshot.data);
    

    到这里:

                AccountUser user = AccountUser.fromDoc(snapshot.data.data());
    

    snapshot.data 返回一个DocumentSnapshot 对象,您可以通过在其上调用.data()DocumentSnapshot 获取数据。

    数据方法

    地图 数据()

    以 Map 形式检索文档中的所有字段。

    AccountHolder.fromDoc 工厂方法中使用它意味着您必须将其参数类型从DocumentSnapshot 更改为Map&lt;String, dynamic&gt;,如下所示:

    改变这个:

    factory AccountHolder.fromDoc(DocumentSnapshot doc) {
       //Rest of code 
    }
    

    到这里:

    factory AccountHolder.fromDoc(Map<String, dynamic> doc) {
       //Rest of code 
    }
    

    【讨论】:

    • 感谢 Victor 抽出宝贵时间帮助我,我按照您的建议进行了尝试,但现在出现错误:类型 '_InternalLinkedHashMap' 不是类型 'DocumentSnapshot'
    • 请提供AccountUser的代码。您需要将AccountUser.fromDoc()方法的参数类型从DocumentSnapshot更改为Map&lt;String, dynamic&gt;
    • 刚刚提供了上面的代码,对不起,我把accountUser改为accountHolder,只是为了澄清。
    • 谢谢,尽管这工作得很好,但我的数据库文件中有很多错误。我试图绕过它而不使用 Map,而是使用文档快照
    • 我通过降级我的 cloudFirestore 包并在工厂构造函数中用 doc.data() 替换 doc 解决了这个问题。
    猜你喜欢
    • 1970-01-01
    • 2021-05-16
    • 2021-09-28
    • 2021-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-21
    • 2021-03-20
    相关资源
    最近更新 更多