错误:类'QuerySnapshot'没有实例获取方法'数据'

问题描述 投票:0回答:1

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

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
flutter dart flutter-layout flutter-dependencies dart-pub
1个回答
0
投票

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

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

  2. snapshot.data.data,我认为这是builder第二行中的错字。

  3. 事实上,您正在对快照进行操作而没有先检查snapshot.hasDatasnapshot.data.documents.length != 0以确保您没有对空快照进行操作。

© www.soinside.com 2019 - 2024. All rights reserved.