无法在Flutter应用中从Cloud Firestore获取数据

时间:2020-11-12 12:49:22

标签: flutter google-cloud-firestore

我已经创建了createRecord()方法:

CollectionReference addBlog = FirebaseFirestore.instance.collection('blogAddress');
  Future<void> createRecord() {
    // Call the user's CollectionReference to add a new user
    return addBlog
        .add({
      'blogAddress': IfUserProfile.blog, // Stokes and Sons
    })
        .then((value) => print("Blog Added"))
        .catchError((error) => print("Failed to add Blog: $error"));
  }

从textformField创建记录:

onChanged: (value) {
                    IfUserProfile.blog = value;
                  },

它可以正常工作,可以在Firestore上正确添加集合: enter image description here

现在,我试图将这些数据放到另一个屏幕中,并调用该实例:

CollectionReference blogAddress =
  FirebaseFirestore.instance.collection('blogAddress');

然后用FutureBuilder将列包装到我拥有数据的位置:

 FutureBuilder<DocumentSnapshot>(
              future: blogAddress.doc().get(),
              builder: (BuildContext context,
                  AsyncSnapshot<DocumentSnapshot> snapshot) {
                if (snapshot.hasError) {
                  return Text("Something went wrong");
                }
                if (snapshot.connectionState == ConnectionState.done) {
                  Map<String, dynamic> data = snapshot.data.data();
                  return Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      SizedBox(
                        height: 30,
                      ),
                      Text("Full Name: ${data['blogAddress']}"),

但是我无法获得数据:

方法'[]'在null上被调用。 接收者:null 尝试致电:

2 个答案:

答案 0 :(得分:1)

snapshot.data.data()返回null。当您请求不存在的文档时,就会发生这种情况。您的代码blogAddress.doc().get()永远不会获得文档,因为没有参数的doc()会生成对尚不存在的随机文档ID的引用。如果需要特定的文档,则应将文档ID传递给doc()

blogAddress.doc("the-document-ID-you-want").get()

答案 1 :(得分:0)

    Try this,
    
      



body: StreamBuilder(
                    stream: Firestore.instance
                        .collection('blogAddress')
                        .snapshots(),
                    builder: (BuildContext context,
                        AsyncSnapshot<QuerySnapshot> snapshot) {
                      if (snapshot.data.toString()=='null')
                        return Text('');
                      if (snapshot.hasError)
                        return new Text('Error: ${snapshot.error}');
                      if(snapshot.data.documents.length<1)
                        return SizedBox.shrink();
                      switch (snapshot.connectionState) {
                        case ConnectionState.waiting:
                          return new Text('Loading...');
                        default:
         
                     Print("snapshotData:- "+snapshot.data.documents.toString());
                          return new ListView(
                            children: snapshot.data.documents
                                .map((DocumentSnapshot document) {
                   Print("documentAll :-"+document.toString());
                      Print("document blogAddress:- "+ document['blogAddress'].toString());      

         
   return Text(document['blogAddress'].toString());
                            }).toList(),
                          );
                      }
                    },
                  )