Flutter 为什么FuffueBu建器认为NULL FixStand文档不是空的?
我在这里使用futureBuilder编写了这段代码,希望当firestore上“已售出”文档的“已售出”字段还没有值时,小部件显示“未售出”(“已售出”文档尚未创建) 但是,当快照明显为空时,因为文档没有创建,所以它会继续使用Triangal的第一个选项,并尝试输出“${snapshot.data['sall']}”,显示此错误Flutter 为什么FuffueBu建器认为NULL FixStand文档不是空的?,flutter,google-cloud-firestore,Flutter,Google Cloud Firestore,我在这里使用futureBuilder编写了这段代码,希望当firestore上“已售出”文档的“已售出”字段还没有值时,小部件显示“未售出”(“已售出”文档尚未创建) 但是,当快照明显为空时,因为文档没有创建,所以它会继续使用Triangal的第一个选项,并尝试输出“${snapshot.data['sall']}”,显示此错误 FutureBuilder(future: Firestore.instance .collection('items')
FutureBuilder(future: Firestore.instance
.collection('items')
.document('itemId')
.collection('sold')
.document('sold')
.get(),
builder: (context, snapshot){
return (snapshot != null) ? Text(
'${snapshot.data['sold']}'
) : Text('not sold');}
看来,从'${SNAPSPOT.DATA [Sale'`] }看来,“快照-数据”为NULL,这是正确的,因为还没有创建文档,但我不能理解为什么它不考虑快照从“(快照!= null)”到NULL。 当创建了“已售出”文档且该文档不为空时,它会在屏幕上显示“${snapshot.data['sall']}”值
只有三元不在这里工作 我搜索了它,并尝试使用“(snapshot.hasdata)”而不是“(snapshot!=null)”,但没有任何更改 我做错了什么 PS-看起来文档实际上是空的,但不知怎么的,它被认为不是空的。我不知道这是firestore方面的问题,还是futureBuilder是为了初始化一些初始数据状态而构建的The method '[]' was called on null.
Receiver: null
Tried calling: []("sold")
另一种方式是:
builder: (context, snapshot){
return (snapshot.data != null) ? Text(
'${snapshot.data['sold']}'
) : Text('not sold');}
您也可以检查错误,也许这就是缺少的
builder: (context, snapshot){
return (snapshot.hasData) ? Text(
'${snapshot.data['sold']}'
) : Text('not sold');}
我同意奥马尔的观点,你可以这样尝试,甚至可以尝试下面的方法
builder: (context, snapshot){
if (snapshot.hasError) return Text("Error");
if (snapshot.hasData) return Text(snapshot.data['sold']);
// else return a progress indicator
return CircularProgressIndicator();
}
我没有。我刚刚重新格式化了代码。使用“snapshot.data”和“snapshot.hasData”可以得到相同的结果。不知何故,它认为空数据不是空的。这就是问题所在。@AndyYoung我用另一个代码片段更新了答案,希望这个代码片段能起作用,你能发现问题所在。它没有什么不同。我认为问题在于它认为空数据不是空的。有什么想法吗?我认为你首先应该确定你的futurebuilder是空的。你试过打印吗?这可能是一个好主意,知道它的内容是什么,然后您可以验证它为什么具有该值
builder: (context, snapshot){
return (snapshot.hasData && snapshot.data != null && snapshot.data.lenght > 0) ? Text(
'${snapshot.data['sold']}'
) : Text('not sold');}