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'

Viewed 208

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!'});
        },
      ),
    );
  }
}


1 Answers

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')
Related