0

Error text:

Null check operator used on a null value The relevant error-causing widget was StreamBuilder<List>

ChangeNotifierProvider(
          create: (context) => getIt<ModelCreator>(),
          child: StreamBuilder(
              stream: getIt<ModelCreator>().chatList(),
              builder: (context, AsyncSnapshot<List<ChatModel>> snapshot) {
                var _data = snapshot.data!; <- The error is here
                return Container();
              }),
        ),

There is no problem with empty Container, that part is full.

Thats getChatList() func

  Stream<List<ChatModel>> getChatList() {
var data = _firebase
    .collection('Users')
    .doc(_fireauth.currentUser!.uid)
    .collection('Chat List')
    .orderBy('Last Message Date', descending: true);

return data.snapshots().map(
    (event) => event.docs.map((e) => ChatModel.fromSnapshot(e)).toList());

}

Thats it ChatModel

  factory ChatModel.fromSnapshot(
  QueryDocumentSnapshot<Map<String, dynamic>> doc) {
return ChatModel(
  id: doc.id,
  image: doc['Image'],
  phone: doc['Phone'],
  photo: doc['Photo'],
  username: doc['Username'],
  createdDate: doc['Created Date'],
  displayMessage: doc['Display Message'],
  lastMessageDate: doc['Last Message Date'],
);

}

1 Answers1

0

the snapshot data can be null. So before accessing the snapshot.data you should check

if(snapshot.hasData)

if it returns true, you can access the data without any issues

Sujan Gainju
  • 4,273
  • 2
  • 14
  • 34