Flutter : Want to show chat message in text widget from cloudFirestore

Viewed 27

I want to show chat messages ( chat text with sender email) in a text widget. what I did is fetch Querysnapshot document data from cloud firestore and try to show that data in the text widget. but when I run the emulator it returns an object instead of text.

Cloud Firestore data:

sender
"rakib@gmail.com"
 (string)
 text
 "hello"

My code :

Column(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: <Widget>[
        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();

                final messageWidget = Text('$messageText');

                messageWidgets.add(messageWidget);
              }
              return Column(
                children: messageWidgets,
              );
            }
            return Text("No widget to build");
          },
        ),

I ALSO TRIED to write

 final messageText = message.data();

this line in this way:

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

BUt it shows "The operator '[]' isn't defined for the type 'Object? Function()'" So, How do I resolve the issue?

1 Answers

You should try this!.

message.get('text').toString(),
Related