flutter - how can I show document?
问题描述
I am trying to fetch document one by one ...i am using below code its work but in console its show me error like StreamBuilderBaseState>#de08b): The getter 'documents' was called on null. Receiver: null Tried calling: documents
new StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection("Quiz").where("topice",isEqualTo: widget.topic).where("section",isEqualTo: widget.section).snapshots(),
builder: (BuildContext context,
AsyncSnapshot<QuerySnapshot> snapshot,) {
int length= snapshot.data.documents.length;
if (!snapshot.hasData)
return new Container(child: Text(""),);
return ListView(
children: <Widget>[
Text(widget.scoren.toString()),
Text(snapshot.data.documents[cunter]["Description"]),
Row(
children: <Widget>[
CupertinoButton(child: Text("COMMENTS"), onPressed: null),
RaisedButton(onPressed: (){
setState(() {
cunter++;
});
// Navigator.of(context).push(new MaterialPageRoute(builder: (context)=>new MyApp()));
},child: Text("NEXT"),)
],
)
],
)
解决方案
You are trying to access the length property of the documents list before you check if the snapshot has any data.
Try inverting those lines, like this
//Check if the snapshot has data
if (!snapshot.hasData) return Container(child: Text(""));
//If you get here it means you have data
int length= snapshot.data.documents.length;
Also, remember that in Dart 2 the new
keyworkd is not needed anymore.
推荐阅读
- c# - 使用 Lucene 搜索子文件夹
- react-native - 反应本机;如何在 FlatList 中放置空间
- c# - 如何让我的 DataGridView 显示所有行而不将它们放入固定大小的容器中?
- c++ - 如何用arduino控制伺服电机
- outlook - 有没有办法在不点击按钮的情况下触发动作
- tensorboard - Why does this code not produce a log that is readable by tensorboard?
- apache-kafka - 应用程序重启后无法从中间主题读取
- javascript - 空合并运算符 (`??`) 给出语法错误
- r - 将最新的 MovieLens 数据集(评分)转换为包含 NA 的矩阵
- java - 运行 Main 类 [main 方法] 期间发生 Eclipse 错误