【问题标题】:"NoSuchMethodError: The getter 'docs' was called on null" Error Flutter Firebase“NoSuchMethodError:在 null 上调用了 getter 'docs'”错误 Flutter Firebase
【发布时间】:2021-03-12 19:12:58
【问题描述】:

每当我尝试使用 .orderby("POST_ID", descending: true 时,我都会收到错误消息“NoSuchMethodError: getter 'docs' was called on null. Receiver: null Tried Calling: docs”。如果有人对我收到此错误的原因以及如何解决它有答案,请提供帮助! 这是我的代码:

          Container(
          margin: EdgeInsets.only(top: 100.0),
          child: StreamBuilder(
              stream: FirebaseFirestore.instance
                  .collection("Cities")
                  .doc(globals.selectedCity)
                  .collection("Posts")
                  .orderBy("POST_ID", descending: true)
                  .where("Category", isEqualTo: globals.selectedCategory)
                  .snapshots(),
              builder: (context, postSnapshot) {
                return ListView.builder(
                  itemCount: postSnapshot.data.docs.length,
                  itemBuilder: (BuildContext context, int index) {
                    switch (postSnapshot.data.docs[index]["Type"]) {
                      case "Image":
                        return new ImageCard(
                          imageLink: postSnapshot.data.docs[index]
                              ["ImageLink"],
                          imageDescription: postSnapshot.data.docs[index]
                              ["ImageDescription"],
                          posterName: postSnapshot.data.docs[index]
                              ["PosterName"],
                        );
                        break;

                      case "Text":
                        return new TextCard(
                          postText: postSnapshot.data.docs[index]
                              ["PostText"],
                          posterName: postSnapshot.data.docs[index]
                              ["PosterName"],
                        );
                        break;

【问题讨论】:

    标签: firebase flutter dart


    【解决方案1】:

    问题就在这里:

        (postSnapshot.data.docs[index]["Type"])
    

    您必须首先确保从 Firestore 获取数据。在连接状态为正在连接或快照有错误的情况下,您在空对象上调用 docs。考虑使用这样的检查构建小部件树:

        class UserInformation extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        CollectionReference users = FirebaseFirestore.instance.collection('users');
    
        return StreamBuilder<QuerySnapshot>(
          stream: users.snapshots(),
          builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.hasError) {
              return Text('Something went wrong');
            }
    
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Text("Loading");
            }
    
            return new ListView(
              children: snapshot.data.docs.map((DocumentSnapshot document) {
                return new ListTile(
                  title: new Text(document.data()['full_name']),
                  subtitle: new Text(document.data()['company']),
                );
              }).toList(),
            );
          },
        );
      }
    }
    

    【讨论】:

    • 所以我这样做了,但它仍然没有订购。它只是吐出“出了点问题”的文字。
    • 您的调试控制台中没有Permission denied 行吗?
    • 并检查您是否没有在 globals.selectedCategory 和 globals.selectedCity 中传递 null
    • 是的,没有 Permission denied 错误并且 globals.selectedCategory 和 globals.selectedCity 都有值。这仅在订购快照时发生。如果快照未排序,则可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 2020-08-11
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 2021-08-14
    • 2020-09-26
    相关资源
    最近更新 更多