【问题标题】:DocumentSnapshot in a FutureBuilder : Bad state: cannot get a field on a DocumentSnapshotPlatform which does not existFutureBuilder 中的 DocumentSnapshot:错误状态:无法获取 DocumentSnapshotPlatform 上不存在的字段
【发布时间】:2021-04-27 22:44:49
【问题描述】:

我一直在努力解决一个错误,这显然是由于我试图在 FutureBuilder 中使用的 DocumentSnapshot 的新更改 你可以在下面看到我是如何使用它的:

body: FutureBuilder(
  future: usersRef.doc(widget.userId).get(),
  builder: ( context,  snapshot) {
    List<Widget> children;
    if (!snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(),
      );
    }
    else if (snapshot.hasData) {
      UserModel user = UserModel.fromDoc(snapshot.data);
factory UserModel.fromDoc(DocumentSnapshot doc) {
  return UserModel(
    id: doc.id,
    name: doc['name'],
    username: doc['username'],
    password: doc['password'],
    profileImageUrl: doc['profileImageUrl'],
    email: doc['email'] ,
    userIds: doc['userIds'] ?? '',
  );
}

我遇到这个错误:

The following StateError was thrown building FutureBuilder<DocumentSnapshot>(dirty, state: _FutureBuilderState<DocumentSnapshot>#46a8d):
Bad state: cannot get a field on a DocumentSnapshotPlatform which does not exist

我已经尝试使用我在这个 github 问题 (https://github.com/FirebaseExtended/flutterfire/issues/3826) 上找到的解决方案来解决它,但它并没有真正解决它。
如果你们中的任何人帮助我,我将不胜感激 谢谢

【问题讨论】:

  • 什么时候出现这个错误?能否请您详细说明一下这个问题。

标签: firebase flutter google-cloud-firestore flutter-web


【解决方案1】:

我遇到了同样的问题,没有找到在访问之前检查字段是否存在的方法,所以我的解决方法如下

factory UserChat.fromDocument(DocumentSnapshot doc) {
  String aboutMe = "";
  String photoUrl = "";
  String nickname = "";
  try {
    aboutMe = doc.get('aboutMe');
  } catch (e) {}
  try {
    photoUrl = doc.get('photoUrl');
  } catch (e) {}
  try {
    nickname = doc.get('nickname');
  } catch (e) {}
  return UserChat(
    id: doc.id,
    photoUrl: photoUrl,
    nickname: nickname,
    aboutMe: aboutMe,
  );
}

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 2021-05-16
    • 2021-06-17
    • 2021-05-19
    • 2021-08-15
    • 1970-01-01
    • 2021-05-16
    • 2021-09-28
    • 2021-09-27
    相关资源
    最近更新 更多