【问题标题】:how can I show document?我怎样才能显示文件?
【发布时间】:2018-09-13 17:34:17
【问题描述】:

我正在尝试一个一个地获取文档...我在下面的代码中使用它的工作,但在控制台中它向我显示错误,例如 StreamBuilderBaseState>#de08b): 在 null 上调用了 getter '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"),)
                  ],
                )

              ],
            )

【问题讨论】:

  • 您是否尝试过构建文档地图,例如“snapshot.data.documents.map”并将数据保存在变量中,然后返回小部件。我认为问题在于“documents [cunter] [“Description”]”行我今天无法对其进行测试,如果找到解决方案,我会继续更新。但首先尝试通过不直接在 StreamBuilder 中返回 ListView 而是使用函数返回小部件来分离代码

标签: flutter google-cloud-firestore


【解决方案1】:

在检查快照是否有任何数据之前,您正在尝试访问文档列表的长度属性。

尝试反转这些行,像这样

//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;

另外,请记住,在 Dart 2 中不再需要 new keyworkd。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-10-26
    • 2021-11-27
    • 2018-09-28
    • 1970-01-01
    • 2021-11-05
    • 2020-10-29
    • 1970-01-01
    相关资源
    最近更新 更多