[英]Flutter Firestore NoSuchMethodError: The method 'get' was called on null
I have a firestore database. 我有一个Firestore数据库。 What I want to know is how to check if a certain Document reference exists in this database. 我想知道的是如何检查此数据库中是否存在某个文档引用。 The code I have used to work but now it's broken. 我曾经工作过的代码,但是现在坏了。
Get reference 获取参考
travelTotalReference =
Firestore.instance.document('$_id/$_presentDate BalanceTotal');
check if data exists 检查数据是否存在
travelTotalReference.get().then((dataSnapshot) {
if (dataSnapshot.exists) {
setState(() {
travel = dataSnapshot.data['total'];
});
}else{
setState(() {
travel = 0;
});
}
});
By the way I know for a fact that this Reference does not exists. 顺便说一下,我知道这个参考文献不存在。 I just want to make sure that if it does i can update the travel variable. 我只想确保如果可以,我可以更新旅行变量。
The error I get 我得到的错误
E/flutter (17184): [ERROR:flutter/shell/common/shell.cc(181)] Dart Error:
Unhandled exception:
E/flutter (17184): NoSuchMethodError: The method 'get' was called on null.
E/flutter (17184): Receiver: null
E/flutter (17184): Tried calling: get()
E/flutter (17184): #0 Object.noSuchMethod
(dart:core/runtime/libobject_patch.dart:48:5)
If you call an object that doesn't exist, how can you expect your travelTotalReference
to be anything but null. 如果调用不存在的对象,那么如何期望travelTotalReference
为null。
Since then calling get()
on it return an error, seems fair... 从那以后调用get()
返回错误,看起来很公平。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.