This error is caused by what? The other files have the same code format, but there are no errors.
I use displayName as the document name.
Error
The method '[]' was called on null.
Receiver: null
Tried calling: [] ("displayName")
final DocumentSnapshot data;
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance
.collection('users')
.document(widget.data['displayName'])
.collection('chat')
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) return LinearProgressIndicator();
return Container();
})
DB:
static Future<void> sendChatToFirebase(
String chatID,
String chatContent,
FirebaseUser currentUser,
DocumentSnapshot data
) async {
Firestore.instance
.collection('users')
.document(data.documentID)
.collection('chat')
.document(chatID)
.setData({
'chatID': chatID,
'chatTimeStamp': DateTime.now().millisecondsSinceEpoch,
'chatContent': chatContent,
'displayName': currentUser.displayName,
'photoUrl': currentUser.photoUrl,
});
}
The error message means, that you are trying to access ['displayName'] from a null value. Somehow your widget.data is null.
It's basically the same as calling null['displayName']
Map<String, dynamic> test; // NULL, but expected as Map / Json / {}
print(test["displayName"]); // throws an error, because what was actually called is null["displayName"]
The index [] operator on the Map class returns null if the key isn't present. This implies that the return type of that operator must be nullable. See null safety . This means that your widget.data['displayName']
may return null
. To fix your issue you need to check that widget.data['displayName]
is not null, if so then you can use the !
to say that widget.data['displayName]
is not null.
widget.data['displayName']!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.