Flutter:未处理的异常:状态错误:DocumentSnapshotPlatform 中不存在字段

Ace*_*Ace 8 firebase flutter google-cloud-firestore

我是 flutter 的新手,我正在通过教程构建一个社交媒体应用程序,我现在正在自定义该应用程序。现在我尝试向用户个人资料页面添加更多输入字段,然后我开始收到以下错误。当我可以登录时,我的时间线页面变成红色警告Bad state: field does not exist within the DocumentSnapshotPlatform

我跑了Flutter clean,现在我的用户无法登录应用程序

我收到此错误:

E/flutter ( 3971): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: Bad state: field does not exist within the DocumentSnapshotPlatform
E/flutter ( 3971): #0      DocumentSnapshotPlatform.get._findKeyValueInMap
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:82
E/flutter ( 3971): #1      DocumentSnapshotPlatform.get._findComponent
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:98
E/flutter ( 3971): #2      DocumentSnapshotPlatform.get
package:cloud_firestore_platform_interface/…/platform_interface/platform_interface_document_snapshot.dart:113
E/flutter ( 3971): #3      DocumentSnapshot.get
package:cloud_firestore/src/document_snapshot.dart:49
E/flutter ( 3971): #4      DocumentSnapshot.[]
package:cloud_firestore/src/document_snapshot.dart:56
E/flutter ( 3971): #5      new User.fromDocument
package:findemed/models/user.dart:46
E/flutter ( 3971): #6      _HomeState.createUserInFirestore
package:findemed/pages/home.dart:152
E/flutter ( 3971): <asynchronous suspension>
E/flutter ( 3971): #7      _HomeState.handleSignIn
package:findemed/pages/home.dart:60
E/flutter ( 3971): #8      _HomeState.initState.<anonymous closure>
package:findemed/pages/home.dart:46
E/flutter ( 3971): #9      _rootRunUnary (dart:async/zone.dart:1198:47)

Run Code Online (Sandbox Code Playgroud)

最初是指向我家文件的这个飞镖部分

buildUsersToFollow() {
  return StreamBuilder(
    stream: usersRef.orderBy('timestamp', descending: true)
    .limit(0)
    .snapshots(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) {
        return circularProgress(context);
      }
      List<UserResult> userResults = [];
      snapshot.data.docs.forEach((doc) {
        User user = User.fromDocument(doc);
        final bool isAuthUser = currentUser.id == user.id;
        final bool isFollowingUser = followingList.contains(user.id);
        // remove auth user from recommended list
        if (isAuthUser) {
          return;
        } else if (isFollowingUser) {
          return;
        } else {
          UserResult userResult = UserResult(user);
          userResults.add(userResult);
        }
        });


Run Code Online (Sandbox Code Playgroud)

现在它指向这个片段:

factory User.fromDocument(DocumentSnapshot doc) {
    return User(
      id: doc['id'],
      email: doc['email'],
      username: doc['username'],
      photoUrl: doc['photoUrl'],
      displayName: doc['displayName'],
      bio: doc['bio'],      
      fullNames: doc['fullNames'],
      practice: doc['practice'],
      speciality: doc['speciality'],
      phone: doc['phone'],
      mobile: doc['mobile'],
      emergency: doc['emergency'],
      address: doc['address'],
      city: doc['city'],
      location: doc['location'],
    );
  }
Run Code Online (Sandbox Code Playgroud)

这是堆栈中指出的另一段代码

currentUser = User.fromDocument(doc);
    print(currentUser);
    print(currentUser.username);
Run Code Online (Sandbox Code Playgroud)

sil*_*orp 61

对于最新版本的cloud_firestore (2.4.0或更高版本)我终于找到了解决方案:

而旧版本的cloud_firestore只需添加??即可解决 最后以避免空值:

factory User.fromDocument(DocumentSnapshot doc) {
   return User(
      id: doc.data()['id'] ?? '',
   );
}
Run Code Online (Sandbox Code Playgroud)

但在新版本中这是不可能的,因为返回Bad 状态

factory User.fromDocument(DocumentSnapshot doc) {
   return User(
       id: doc.get('id')
   );
}
Run Code Online (Sandbox Code Playgroud)

只需检查文档是否包含密钥即可解决此问题:

factory User.fromDocument(DocumentSnapshot doc) {
   return User(
      id: doc.data().toString().contains('id') ? doc.get('id') : '', //String
      amount: doc.data().toString().contains('amount') ? doc.get('amount') : 0,//Number
      enable: doc.data().toString().contains('enable') ? doc.get('enable') : false,//Boolean
      tags: doc.data().toString().contains('tags') ? doc.get('tags').entries.map((e) => TagModel(name: e.key, value: e.value)).toList() : [],//List<dynamic>
   );
}
Run Code Online (Sandbox Code Playgroud)

希望这有帮助,问候!:)

  • 这应该是最新 firestore 的最终答案 (4认同)

小智 18

将您的工厂更改为:

factory User.fromDocument(DocumentSnapshot doc) {
  return User(
    id: doc.data()['id'],
    email: doc.data()['email'],
    username: doc.data()['username'],
    photoUrl: doc.data()['photoUrl'],
    displayName: doc.data()['displayName'],
    bio: doc.data()['bio'],      
    fullNames: doc.data()['fullNames'],
    practice: doc.data()['practice'],
    speciality: doc.data()['speciality'],
    phone: doc.data()['phone'],
    mobile: doc.data()['mobile'],
    emergency: doc.data()['emergency'],
    address: doc.data()['address'],
    city: doc.data()['city'],
    location: doc.data()['location'],
  );
}
Run Code Online (Sandbox Code Playgroud)

  • 我试过这个,但它给出了运算符“[]”不是为“对象”类型定义的。 (2认同)

小智 10

我遇到了同样的问题,我通过询问参数是否存在来解决它。

这是我之前的代码:

if(widget.document['imageUrl'] == null)//here was my error, I was calling a field that did not exist in the document
  Container(
    child:Icon(
      Icons.image,
      size: 20.0,
      color: Colors.grey,
    ),
  ),
Run Code Online (Sandbox Code Playgroud)

这对我来说是这样的:

if(widget.document.data().containsKey('imageUrl'))
  Container(
    child:Icon(
      Icons.image,
      size: 20.0,
      color: Colors.grey,
    ),
  ),
Run Code Online (Sandbox Code Playgroud)


小智 7

在构建器方法中显示文档数据时,传递文档中不存在的错误属性名称也可能会发生此问题。这为我解决了这个问题。