简体   繁体   English

Flutter 错误:无法访问从 Firebase Firestore 获取的快照内的数据

[英]Flutter error : Failure to access data inside a snapshot fetched from Firebase Firestore

Using Flutter 3.3.9, I fetch a record from my Firestore database using a Streambuilder.使用 Flutter 3.3.9,我使用 Streambuilder 从我的 Firestore 数据库中获取一条记录。 I use the following code segment to do this:我使用以下代码段来执行此操作:

              StreamBuilder<Object>(
                stream: FirebaseFirestore.instance
                    .collection('users')
                    .doc(userId)
                    .snapshots(),
                builder: (context, snapshot) {
                  if (snapshot.connectionState == ConnectionState.waiting) {
                    return Text('Loading...');
                  }
                  return Text(
                    snapshot.data!.doc['username'], // This line is marked as error bexcause "doc" is illegal.
                    ),
                  );
                },
              ),

The snapshot.data..doc['username'] gives the following error: snapshot.data..doc['username']给出以下错误:

The getter 'doc' isn't defined for the type 'Object'.没有为类型“Object”定义 getter“doc”。

I verified that the 'Object' is of type "AsyncSnapshot" (=snapshot.runtimeType).我验证了“对象”的类型是“AsyncSnapshot”(=snapshot.runtimeType)。 It looks like the only getters available for the snapshot.data are hashCode, runtimeType, toString(), and noSuchMethod(..).看起来可用于 snapshot.data 的唯一 getter 是 hashCode、runtimeType、toString() 和 noSuchMethod(..)。

I tried我试过

snapshot.data!().doc['username'],

But this does not work either.但这也不起作用。 The error is "The expression doesn't evaluate to a function, so it can't be invoked"错误是“表达式未计算为 function,因此无法调用”

I was able to access the data without using the StreamBuilder.我能够在不使用 StreamBuilder 的情况下访问数据。 The following works:以下作品:

                  final docRef = FirebaseFirestore.instance
                      .collection('users')
                      .doc(userId);

                  docRef.get().then(
                    (DocumentSnapshot doc) {
                      final data = doc.data() as Map<String, dynamic>;
                      print(data['username']);
                    },
                    onError: (e) => print("Error getting document: $e"),
                  );

you have two mistakes, in your piece of code, you should specify the type of the AsyncSnapshot , like this:你有两个错误,在你的代码中,你应该指定AsyncSnapshot的类型,如下所示:

 StreamBuilder<DocumentSnapshot>( // Specified type
                stream: FirebaseFirestore.instance
                    .collection('users')
                    .doc(userId)
                    .snapshots(),
                builder: (context, AsyncSnapshot<DocumentSnapshot>  snapshot) { //Specified type
    //...

now using snapshot.data!现在使用snapshot.data! , it should be a DocumentSnapshot type, and as I see, that you're trying to get the data of that document so you need also to change this line: ,它应该是DocumentSnapshot类型,正如我所见,您正在尝试获取该文档的数据,因此您还需要更改此行:

  snapshot.data!.doc['username'],

to this:对此:

(snapshot.data!.data() as Map<String, dynamic>)['username'],

now it will access the username field properly.现在它将正确访问用户名字段。

You define your StreamBuilder's type in wrong way, change it to this:您以错误的方式定义了 StreamBuilder 的类型,将其更改为:

StreamBuilder<AsyncSnapshot>(

   ... 
)

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

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