![](/img/trans.png)
[英]The method 'toDate' was called on null. Receiver: null Tried calling: toDate()
[英]The method '[]' was called on null. Receiver: null Tried calling: []("displayName")
這個錯誤是由什么引起的? 其他文件的代碼格式相同,但沒有錯誤。
我使用 displayName 作為文檔名稱。
錯誤
在 null 上調用了方法“[]”。
收件人:null
嘗試調用:[] ("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();
})
D B:
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,
});
}
該錯誤消息表示您正在嘗試從 null 值訪問 ['displayName']。 不知何故你的 widget.data 是 null。
它與調用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"]
如果鍵不存在,則 Map class 上的索引 [] 運算符返回 null。 這意味着該運算符的返回類型必須可以為空。 安全見null 。 這意味着您的widget.data['displayName']
可能會返回null
。 要解決您的問題,您需要檢查widget.data['displayName]
是否不是 null,如果是,那么您可以使用!
說widget.data['displayName]
不是 null。
widget.data['displayName']!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.