首页 > 解决方案 > 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"),)
                  ],
                )

              ],
            )

标签: fluttergoogle-cloud-firestore

解决方案


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.


推荐阅读