首页 > 解决方案 > 集合中没有数据时显示占位符

问题描述

Text当集合为空时,我试图显示一个小部件。Text('No Events :(');但是,即使我的收藏为空,我也永远无法满足展示的条件。我认为这是飞镖语法错误?

 Container(
                  height: 400,
                   // width: 500,
                    child: StreamBuilder<QuerySnapshot>(
                      stream: Firestore.instance.collection('Events').where("bandId", isEqualTo:identifier ).snapshots(),
                      builder: (BuildContext context,
                          AsyncSnapshot<QuerySnapshot> snapshot) {

                        if (!snapshot.hasData)
                          return new Text('No Events :(');
                        switch (snapshot.connectionState) {
                          case ConnectionState.waiting:
                            return new Text('Loading...');
                          default:
                            return new ListView(
                              children: snapshot.data.documents
                                  .map((DocumentSnapshot document) {

                                return  Dismissible(
                                  key: new Key(document.documentID),
                                  onDismissed: (direction){
                                    Firestore.instance.runTransaction((transaction) async {
                                      DocumentSnapshot snapshot=
                                          await transaction.get(document.reference);
                                          await transaction.delete(snapshot.reference);


                                    });
                                    Fluttertoast.showToast(msg: "Event Deleted");
                                  },

                                  child: CustomCard(
                                    event: document['event'],
                                    location: document['location'],
                                    service: document['service'],
                                    date: document['date'],
                                  ),
                                );
                              }).toList(),
                            );
                        }
                      },
                    )),

标签: fluttergoogle-cloud-firestore

解决方案


您正在创建语法错误。您不能使用 new Text("Something"); new ListView( // ) 你必须指定一个支持儿童的小部件。您可以使用 Column 来显示多个小部件。

原因:您正在指定 If 和 Switch 大小写。我建议仅在 Switch case 或 If-Else 条件下覆盖您的逻辑。


Container(
  height: 400,
  // width: 500,
  child: StreamBuilder(
    stream: Firestore.instance.collection('Events').where("bandId", isEqualTo:identifier ).snapshots(),
    builder: (BuildContext context,
      AsyncSnapshot snapshot) {
      return new Column(
        children: [
        if (!snapshot.hasData)
          new Text('No Events :('),
        switch (snapshot.connectionState) {
          case ConnectionState.waiting:
              new Text('Loading...');
          default:
            new Container(
              childe: ListView(
              children: snapshot.data.documents
                .map((DocumentSnapshot document) {
                  return  Dismissible(
                    key: new Key(document.documentID),
                    onDismissed: (direction){
                      Firestore.instance.runTransaction((transaction) async {
                        DocumentSnapshot snapshot=
                            await transaction.get(document.reference);
                            await transaction.delete(snapshot.reference);
                        });
                      Fluttertoast.showToast(msg: "Event Deleted");
                    },
                    child: CustomCard(
                      event: document['event'],
                      location: document['location'],
                      service: document['service'],
                      date: document['date'],
                    ),
                  );
                }).toList(),
              ),
            ),
        }
      ]),
    },
  )
);


推荐阅读