Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
279 views
in Technique[技术] by (71.8m points)

flutter - How to access to data inside a document

How can I access to data inside a document when inside that document is a subcollection and the data field I want?. For example I would like to access to the field "name"

enter image description here

I've already tried this:

FirebaseFirestore.instance
        .collection('Rooms')
        .doc('8w0TE4D04xyeGgL3xAyk') //ID from specific room (RoomA,RoomB...)
        .get()
        .then((QuerySnapshot querySnapshot) => {
          querySnapshot.docs.forEach((doc) {
            print(doc["name"]);
          });
        });

This is the error that gives me in console:

error: The argument type 'Set Function(QuerySnapshot)' can't be assigned to the parameter type 'FutureOr Function(DocumentSnapshot)'. (argument_type_not_assignable at [proyectoihc2] libdatabase.dart:66)

question from:https://stackoverflow.com/questions/65878526/how-to-access-to-data-inside-a-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Here's what you're doing wrong,

  • This isn't javaScript, the way you use arrow syntax is different,

Either do this

(value){
...
}

or use this if your function has only one line

(value) => print(value);

In dart just {} will be treated like the data structure Set hence, in this case, you're assigning the function to a Set, hence the error.

Other pointers:-

  • You're calling a for loop on the QuerySnapshot which is wrong because you're fetching a single document and not a collection of documents.

  • the way you access a key in the latest version of firebase plugin is this way

doc.data()['name']

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...