【问题标题】:Flutter: How to show a message if snapshot has no dataFlutter:如果快照没有数据,如何显示消息
【发布时间】:2020-04-02 10:15:44
【问题描述】:

我在颤振中有一个简单的问题,但我不知道如何解决它。所以就在这里。如果我正在调用的快照在我的 firebase 数据库中没有数据,我会尝试在我的应用中显示一条消息。

我有这个代码:

return Scaffold(
  body: Container (
    child: new LayoutBuilder(
      builder: (BuildContext context, BoxConstraints viewportConstraints) {
        return Column(
          children: <Widget>[
            SizedBox(
              height: MediaQuery.of(context).size.height * 0.020,
            ),
            SingleChildScrollView(
              scrollDirection: Axis.vertical,
              child: Container(
                child: Column(
                  children: <Widget>[
                    StreamBuilder<QuerySnapshot>(
                        stream: db.collection('CONFIRMED HELP BENEFICIARY').where('Respondents_ID', isEqualTo: '${widget.UidUser}').snapshots(),
                        builder: (context, snapshot) {
                          if (snapshot.hasData) {
                            return Column(
                                children: snapshot.data.documents
                                    .map((doc) => buildItem(doc))
                                    .toList());
                          }
                          else {
                            return Container(
                                color: Colors.red,
                                height: 200,
                                width: 200,
                                child: Text("No Data"));

            )
          ],
        );
      },
    ),
  ),
);

在我的单滚动视图中,我有一个流构建器。还有一个if else。因此,如果“snapshot.hasdata”我正在显示一个数据列表并且它成功地显示了这一点。但问题在于“其他”。我一直在尝试显示一个容器,该容器具有颜色:红色和包含“无数据”的文本,但我完全不知道如何@@ 它显示容器几毫秒然后消失 @@。请帮忙。

【问题讨论】:

  • 尝试使用 if (snapshot.hasData && !snapshot.data.isEmpty)
  • 还是不行,先生:(

标签: flutter google-cloud-firestore


【解决方案1】:
 if(!snapshot.hasData){
          // still waiting for data to come 
          return circularProgress();

        }
        else if(snapshot.hasData && snapshot.data.isEmpty) {
          // got data from snapshot but it is empty
         
            return Text("no data");
          }
        else  {
        
            // got data and it is not empty 
            return ListView(
              children: snapshot.data,
            );
          }
      },

【讨论】:

    【解决方案2】:

    缺少几个括号。这导致了问题。我为你修复了代码。

     return Scaffold(
      body: Container(
        child: new LayoutBuilder(
          builder: (BuildContext context, BoxConstraints viewportConstraints) {
            return Column(
              children: <Widget>[
                SizedBox(
                  height: MediaQuery.of(context).size.height * 0.020,
                ),
                SingleChildScrollView(
                  scrollDirection: Axis.vertical,
                  child: Container(
                    child: Column(
                      children: <Widget>[
                        StreamBuilder<QuerySnapshot>(
                            stream: db.collection('CONFIRMED HELP BENEFICIARY')
                                .where('Respondents_ID', isEqualTo: '${widget.UidUser}')
                                .snapshots(),
                            builder: (context, snapshot) {
                              if (snapshot.hasData) {
                                return Column(
                                    children: snapshot.data.documents
                                        .map((doc) => buildItem(doc))
                                        .toList());
                              }
                              else {
                                return Container(
                                    color: Colors.red,
                                    height: 200,
                                    width: 200,
                                    child: Text("No Data"));
                              }
                            }
                        )
                      ],
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
    

    【讨论】:

    • 还是一样的先生 :( 它显示容器就像一毫秒然后它消失了@@
    • 我认为这意味着响应已到达,没有什么可显示的
    • 尝试打印返回的数据
    猜你喜欢
    • 2021-04-02
    • 2020-11-28
    • 2021-11-17
    • 2021-07-01
    • 2019-08-05
    • 2020-02-19
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    相关资源
    最近更新 更多