![](/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: [](“color”)
我想從 Firestore 中檢索顏色。 但每次我這樣做時,我都會收到以下錯誤:
The method '[]' was called on null.
Receiver: null
Tried calling: []("color")
這是我的代碼:
bool cardColor = false;
String _userId;
@override
void initState() {
super.initState();
checkIfColorOrNot();
}
checkIfColorOrNot() async {
FirebaseAuth.instance.currentUser().then((user) {
_userId = user.uid;
});
DocumentSnapshot ds = await Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(_userId)
.get();
this.setState(() {
cardColor = ds.exists; // If the above if exists then cardColor is turned true else it stays flase
});
}
_cardColorApply(child) {
FirebaseAuth.instance.currentUser().then((user) {
_userId = user.uid;
});
return StreamBuilder(
stream: cardColor
? Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(_userId)
.snapshots() // this should be shows only when cardColor is true
: Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.snapshots(), // this should be shows only when cardColor is false
builder: (context, snapshot) {
//Check to make sure snapshot.hasData has data or not
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
int colorValue = int.parse(snapshot.data['color']);
return Card(
color: Color(colorValue),
child: child,
);
},
);
}
@override
Widget build(BuildContext context) {
return InkWell(
onTap: widget.onTap,
child: _cardColorApply(_listItems()),
);
}
我為此使用 statefull 小部件。 最后添加文檔(_userId)下的信息,因此最初將是 null ,因此當其 null 想要訪問文檔(widget.rackBookItems.id)以獲取顏色信息時。 讓我知道是否需要更多信息來獲得解決方案。
當 cardColor = false 我的數據庫將是這樣的,所以它可以從文檔中訪問顏色(widget.rackBookItems.id)
完成一些任務后,數據庫更改為以下一個,因此 cardColor 更改為 true 並且可以從 document(_userId) 訪問顏色
_userId
正在返回 null,這就是您沒有獲得任何數據的原因。 您需要創建以下方法:
Stream<DocumentSnapshot> getUserDocument() async* {
FirebaseUser user = await getCurrentUser();
yield* Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(user.uid)
.snapshots();
}
Stream<DocumentSnapshot> getRackDocument() async* {
yield* Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.snapshots();
}
Future<FirebaseUser> getCurrentUser() async {
return await FirebaseAuth.instance.currentUser();
}
然后在checkIfColorOrNot()
中添加回調中文檔的檢索,以確保它在檢索到userId
后得到執行:
checkIfColorOrNot() async {
FirebaseAuth.instance.currentUser().then((user) {
_userId = user.uid;
DocumentSnapshot ds = await Firestore.instance
.collection('rackBookItems')
.document(widget.rackBookItems.id)
.collection('user')
.document(_userId)
.get();
this.setState(() {
cardColor = ds.exists; // If the above if exists then cardColor is turned true else it stays flase
});
});
}
在StreamBuilder
中執行以下操作:
_cardColorApply(child) {
return StreamBuilder(
stream: cardColor ? getUserDocument() : getRackDocument(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return CircularProgressIndicator();
}
else if(snapshot.hasData){
int colorValue = int.parse(snapshot.data['color']);
return Card(
color: Color(colorValue),
child: child,
);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.