【问题标题】:Can't Get data from Cloud Firestore in flutter app无法在 Flutter 应用中从 Cloud Firestore 获取数据
【发布时间】:2021-02-24 11:27:58
【问题描述】:

我已经创建了 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 上正确添加集合:

现在我正在尝试将这些数据放入另一个屏幕并调用实例:

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 上调用了方法“[]”。 接收方:空 尝试调用:

【问题讨论】:

    标签: flutter google-cloud-firestore


    【解决方案1】:

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

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

    【讨论】:

    • 好吧,这很清楚,但如果进入 createRecord doc(id) 是由 firestore 随机创建的,那么我必须在 get 类中放入 doc() 吗?
    • 我不清楚你在问什么。如果您有新问题,我建议您将其与不符合您预期的代码一起单独发布。
    • 我添加了获取类 return addBlog .doc('ABC123') .set({ 'full_name': IfUserProfile.blog,在这种特定情况下,文档的 ID 是“ABC123”,它如果我将 ABC123 作为 ID 可以工作,但我想拥有自己生成的 ID 如何获得它?
    • 请发布一个新问题以更详细地解释您遇到的问题。
    【解决方案2】:
        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(),
                              );
                          }
                        },
                      )
    

    【讨论】:

    • 你试试这个你的主体吗?
    • 再检查一下,我添加了打印语句以获取您的工作步数。
    猜你喜欢
    • 2022-06-24
    • 2023-03-15
    • 2022-09-23
    • 2020-05-10
    • 2020-05-11
    • 2018-12-20
    • 2019-10-29
    • 1970-01-01
    相关资源
    最近更新 更多