A non-null String must be provided to a Text widget. 'package:flutter/src/widgets/text.dart': Failed assertion: line 370 pos 10: 'data != null'

188

Your documents[i]['text'] is null, and Text widget requires a String parameter. Check it before using it and set a default value. It will look like this:

Text(documents[i]['text'] ?? 'Text is null')
Share:
188
satyendra singh
Author by

satyendra singh

A passionate Developer crazy about exploring new technologies in web and mobile app(Flutter) development and many more.

Updated on December 25, 2022

Comments

  • satyendra singh
    satyendra singh over 1 year
    
    import 'package:cloud_firestore/cloud_firestore.dart';
    
    
    class ChatScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: StreamBuilder(
              stream: Firestore.instance
                  .collection('chats/qlxlom4AuKMJNjSo3w4a/messages')
                  .snapshots(),
              builder: (ctx, streamSnapShot) {
                if (streamSnapShot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: CircularProgressIndicator(),
                  );
                }
                final documents = streamSnapShot.data.documents;
                return ListView.builder(
                  itemCount: documents.length,
                  itemBuilder: (ctx, i) => Container(
                    padding: EdgeInsets.all(8),
                    child: Text(documents[i]['text']),
                  ),
                );
              }),
          floatingActionButton: FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: () {
              Firestore.instance
                  .collection('chats/qlxlom4AuKMJNjSo3w4a/messages')
                  .add({'text': 'this was addded bi clicking the button!'});
            },
          ),
        );
      }
    }
    
    
    
    • Connell.O'Donnell
      Connell.O'Donnell over 3 years
      Please include a description of the problem. Your error message mentions line 370, please mention which line in the code that is.