Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Angela Lu course

The method ‘[]’ can’t be unconditionally invoked because the receiver can be ‘null’.
Try making the call conditional (using ‘?.’) or adding a null check to the target (‘!’).

I’m getting this error while trying to stream data from firebase cloud
I don’t know what to do

StreamBuilder<QuerySnapshot>(
              stream: _firestore.collection('messages').snapshots(),
              builder: (context, snapshot) {
                List<Text> messageWidgets = [];
                if (snapshot.hasData) {
                  final messages = snapshot.data?.docs;

                  for (var message in messages!) {
                    final messageData = message.data();
                    final messageText = messageData['text'];
                    final messageSender = messageData['sender'];
                    final messageWidget =
                        Text('$messageSender said $messageText');
                    messageWidgets.add(messageWidget);
                  }
                }
                return Column(
                  children: messageWidgets,
                );
              },
            ),

the error in those two lines

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

 final messageText = messageData['text'];
 final messageSender = messageData['sender'];

trying to get and stream data from firbase cloud

>Solution :

You can use as prefix to object to Map. Then you can use keys to get the data.

final messageData = message.data() as Map?;
final messageText = messageData?['text']??"default msg";
final messageSender = messageData?['sender']??"default sender";
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading