【问题标题】:Insert a Text() if a Grid List is Empty in Flutter如果 Flutter 中的网格列表为空,则插入 Text()
【发布时间】:2019-12-06 17:51:38
【问题描述】:

我正在开发一个 Flutter 应用程序,在我的 Main 路由中,我有一个 Grid List,其中填充了由数据库检索的元素。 当 Grid 为空时,我想布局 Text() 消息,或类似的东西, 永久。 我用

填充我的列表
list() {
   return Container(
      child: FutureBuilder(
        future: pets,
        builder: (context, snapshot) {
          if (!snapshot.hasData || snapshot.data == null) {
            return emptyGrid();
          }
          else
          return imageGrid(snapshot.data);
        },
      ),
    );
  }

emptyGrid 在哪里

Container emptyGrid() {
    return Container(
      child: Center(
        child: Column(
          children: <Widget>[
            Text("No Data Found")
          ],
        ),
      ),
    );
  }

问题是,emptyGrid 布局正确,但之后立即消失。

【问题讨论】:

  • 听起来您的数据库正在返回数据。你打印出快照的值了吗?
  • 你必须在Widget build方法中这样做
  • @EricDuffett 我打印了。我的数据库是空的。 “没有数据”的布局,但只有一秒钟

标签: list flutter grid future


【解决方案1】:

你需要修改你的list()如下:

您需要检查列表长度是否为零。

list() {
  return Container(
    child: FutureBuilder(
      future: pets,
      builder: (context, snapshot) {
        if (!snapshot.hasData || snapshot.data == null) {
          return Text('Loading.....');
        } else if (snapshot.data.length == 0) {
          return emptyGrid();
        } else {
          return imageGrid(snapshot.data);
        }
      },
    ),
  );
}

【讨论】:

    【解决方案2】:

    试试这个:

    Widget build(BuildContext context) {
        return new Container(
          child: new FutureBuilder(
              future: FirebaseDatabase.instance.reference().child("table").once(),
              builder: (BuildContext context, AsyncSnapshot snapshot) {
                if (snapshot.hasData) {
                  if (snapshot.data!=null) {
                    return new Column (
                      children: imageGrid(snapshot.data),
                    );
                  } else {
                    return Container(
                      child: Center(
                        child: Column(
                          children: <Widget>[
                            Text("No Data Found")
                          ],
                        ),
                      ),
                    );
                  }
                }
              }
            )
          );
      }
    

    【讨论】:

    • 我把它放在一个Widget里面,但问题又来了!
    • 同上:Text("No Data Found") 仅显示一秒钟,不会保留在屏幕上
    • 你有没有在你的构建方法中尝试过上面的代码?然后显示什么屏幕?
    • 是的。显示“未找到数据”一秒钟,然后消失。
    猜你喜欢
    • 1970-01-01
    • 2020-08-29
    • 2012-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多