0

I am trying to use

StreamBuilder<QuerySnapshot>( stream: _firestore.collection('messages').snapshots(), builder: (context,snapshot){ if(snapshot.hasData) { final messages = snapshot.data.docs; List<Text> messageWidgets=[]; for(var message in messages){ final messageText=message.data['text']; final messageSender=message.data['sender']; final messageWidget= Text('$messageText from $messageSender'); messageWidgets.add(messageWidget); } } }, ), I am getting error in final messageText=message.data['text']; final messageSender=message.data['sender']; 

I know it may be because of the new update but I want to access it and in documentation could not find anything like this.

In database in document I have two fields sender and text that is what I want to get individually out of data in messageText and messageSender. also please suggest some source where I could read so that next time I would not need to ask such question.

1
  • whats the error message? Commented Aug 8, 2021 at 23:00

1 Answer 1

1

Change these lines:

final messageText=message.data['text']; final messageSender=message.data['sender']; 

to:

final messageText=(message.data() as Map<String, dynamic>)['text']; final messageSender=(message.data() as Map<String, dynamic>)['sender']; 

The data contained in a QueryDocumentSnapshot can be retrieved by calling the .data() method.

Sign up to request clarification or add additional context in comments.

6 Comments

Thank you for your time but that's not working. I used get('text) it worked fine.
That's alright. When you said "that's not working", what error did you get?
It just showed red line under ['text'] and failed to run
Can you hover your mouse on the red line and see what the actual error is? The red lines always have a corresponding error.
Yes sure, The operator '[]' isn't defined for the type 'Object'. (Documentation) Try defining the operator '[]'.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.