错误:类“ QuerySnapshot”没有实例获取器“数据”

时间:2020-06-05 16:07:21

标签: flutter dart flutter-layout flutter-dependencies dart-pub

我正在尝试创建一个包含3个选项卡的UI屏幕。 LatestItem,ReviewItem和Profile。但是,最近项目小部件中存在一些后端问题。显示错误:类'QuerySnapshot'没有实例获取器'data'。 附言:整个代码很大,因此我为整个代码共享了一个文档:https://docs.google.com/document/d/1qs4ajPJ0DBjserBJ3iBZmPXPz1zTP7tIYSh8vceVQn8/edit?usp=sharing LatestItems():

Widget RecentItems() {
    return Padding(
      padding: const EdgeInsets.all(10.0),
      child: StreamBuilder(
          stream: Firestore.instance
              .collection("users")
              .document(uid)
              .collection("recent")
              .snapshots(),
          builder: (context, snapshot) {
            print(snapshot.data);
            List orders = List.from(Map.from(snapshot.data.data)['orders']);
            Map order;
            for (int i = 0; i < orders.length; i++) {
              if (orders[i]['orderId'] == widget.map['orderId'] &&
                  orders[i]['homemaker'] == widget.map['homemaker']) {
                order = orders[i];
                break;
              }
            }
            if (snapshot.data.isEmpty) {
              return Center(
                  child:
                  Text("OOPS, Looks like no one is serving!"));
            }
            print(order);
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: CircularProgressIndicator());
            } else if (snapshot.hasData) {
              print(snapshot.data.documents[0].data);
              return Container(
                height: 400,
                child: ListView.builder(
                    itemCount: snapshot.data.documents.length,
                    itemBuilder: (BuildContext context, int index) {
                      return Container(
                        margin: EdgeInsets.all(10.0),
                        width: MediaQuery
                            .of(context)
                            .size
                            .width,
                        height: 85,
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.0),),
                        child: Padding(
                          padding: const EdgeInsets.all(10.0),
                          child: Column(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Expanded(child: Text(
                                    "${snapshot.data.documents[index]
                                        .data["dishname"]}", style: TextStyle(
                                      fontSize: 15,
                                      fontWeight: FontWeight.bold),)),
                                  //Icon: how to access if food is veg or not
                                ],
                              ),
                              // SizedBox(height:5),
                              Row(
                                children: <Widget>[
                                  Expanded(child: Text(
                                    "${snapshot.data.documents[index]
                                        .data["homemaker"]}",
                                    style: TextStyle(fontSize: 10),)),
                                  Text("${snapshot.data.documents[index]
                                      .data["rating"]}",
                                      style: TextStyle(fontSize: 15)),
                                  Icon(
                                    Icons.star, color: Colors.yellow.shade800,
                                    size: 20,)
                                ],
                              ),
                              SizedBox(height: 5),
                              //How to access order date
                              Text(
                                "Ordered ${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .day}/${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .month}/${DateTime
                                    .parse(order['order_placed_at']
                                    .toDate()
                                    .toString())
                                    .year}}",
                                style: TextStyle(fontSize: 15.0,
                                    fontWeight: FontWeight.bold),
                              ),
                            ],
                          ),
                        ),
                      );
                    }),
              );
            } //
          }),
    );
  }

错误消息是:

The getter 'data' was called on null.
Receiver: null
Tried calling: data
The relevant error-causing widget was: 
  StreamBuilder<QuerySnapshot> file:///C:/Flutter/Naniz_eats/lib/UserProfilePage.dart:434:14
════════════════════════════════════════════════════════════════════════════════════════════════════
I/flutter (28940): Instance of 'QuerySnapshot'

════════ (3) Exception caught by widgets library ═══════════════════════════════════════════════════
Class 'QuerySnapshot' has no instance getter 'data'.
Receiver: Instance of 'QuerySnapshot'
Tried calling: data
The relevant error-causing widget was: 
  StreamBuilder<QuerySnapshot> file:///C:/Flutter/Naniz_eats/lib/UserProfilePage.dart:434:14

2 个答案:

答案 0 :(得分:0)

可能有几件事或所有原因导致

  1. print第一行中的builder。如果snapshot确实为空,则您已经在调用数据而无需先检查其是否为空。

  2. snapshot.data.data,我认为是builder

  3. 第二行中的错字
  4. 您在不首先检查snapshot.hasDatasnapshot.data.documents.length != 0以确保您没有对空快照进行操作的情况下对快照进行操作的事实。

您还应该能够通过按以下错误消息来专门检查导致错误的行,其中一条错误消息应包含指向特定行的链接(问题中未显示,应在较长的字符之间一堆错误消息)

答案 1 :(得分:0)

此代码:

Firestore.instance
              .collection("users")
              .document(uid)
              .collection("recent")
              .snapshots()

返回类型为Stream的{​​{1}},问题出在这里:

QuerySnapshot

代码 List orders = List.from(Map.from(snapshot.data.data)['orders']); 将返回snapshot.data的实例,而QuerySnapshot不包含名为QuerySnapshot的实例变量。因此,如果要列出文档列表,则必须执行以下操作:

data

https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/cloud_firestore/lib/src/query_snapshot.dart#L17