我试图从 cloud firestore 获取文档,但我无法获取,因为“未为类型 'QuerySnapshot' 定义 getter 'documents'

时间:2021-03-23 11:26:41

标签: flutter google-cloud-firestore

addData(){
    Map<String, dynamic> Demodata = {"Name": "The growing developer",
    "motto":"Let's grow and learn together"
    };

    CollectionReference collectionReference = FirebaseFirestore.instance.collection('Patients');
    collectionReference.add(Demodata);
  }
  FectchData(){
    CollectionReference collectionReference = FirebaseFirestore.instance.collection('Patients');
    collectionReference.snapshots().listen((snapshot) {

      List documents;
      setState(() {
        documents = snapshot.**documents**;<---- the word 'document' is uderlined 
                                                 by red color line
      });
    });
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Create a new record"),
        centerTitle: true,
      ),

      body: Center(
        child: Column(
          children: <Widget>[
            ElevatedButton(onPressed: FectchData
                , child: Text(
                  "Patient details",
                )),
            ElevatedButton(onPressed: (){

            }, child: Text(
              "Contact Details",
            )),
            ElevatedButton(onPressed: (){

            }, child: Text(
              "Allergies/Adverse drug reaction",
            )),ElevatedButton(onPressed: (){

            }, child: Text(
              "Immunisation Status",
            )),
            ElevatedButton(onPressed: (){}, child:
            Text(
              "Pregnancy details",
            )),

            ElevatedButton(onPressed: (){}, child:
            Text(
              "BreastFeeding details",
            )),
            ElevatedButton(onPressed: (){}, child:
            Text(
              "Social History details",
            )),
            ElevatedButton(onPressed: (){}, child:
            Text(
              "Past Medical History",
            )),
            ElevatedButton(onPressed: (){}, child:
            Text(
              "Medications and Supplements",
            )),

          ],
        ),
      ),
    );
  }
}

2 个答案:

答案 0 :(得分:0)

要从 snapshots() 获取文档列表,您需要调用 data docs

    snapshot.data.docs

请注意,您没有处理错误和连接状态,并且您的代码容易出错。您可以在 documentation 中阅读更多内容。

答案 1 :(得分:0)

我建议您使用 StreamBuilder 以便更轻松地处理流的状态。你可以这样试试:

StreamBuilder<QuerySnapshot>(
  stream: collectionReference.snapshots(),
  builder: (context, snapshot) {
    if (!snashot.hasData) {
      // Do something...
    }
    var _documents = snapshot.data.docs;
    return Center(
      child: Column(
        children: <Widget>[
          ElevatedButton(onPressed: (){}
              , child: Text(
                "Patient details",
              )),
          ElevatedButton(onPressed: (){

          }, child: Text(
            "Contact Details",
          )),
          ElevatedButton(onPressed: (){

          }, child: Text(
            "Allergies/Adverse drug reaction",
          )),ElevatedButton(onPressed: (){

          }, child: Text(
            "Immunisation Status",
          )),
          ElevatedButton(onPressed: (){}, child:
          Text(
            "Pregnancy details",
          )),

          ElevatedButton(onPressed: (){}, child:
          Text(
            "BreastFeeding details",
          )),
          ElevatedButton(onPressed: (){}, child:
          Text(
            "Social History details",
          )),
          ElevatedButton(onPressed: (){}, child:
          Text(
            "Past Medical History",
          )),
          ElevatedButton(onPressed: (){}, child:
          Text(
            "Medications and Supplements",
          )),
        ],
      ),
    );
  },
),

关于StreamBuilderhere