【问题标题】:Removing button and load data移除按钮并加载数据
【发布时间】:2022-01-21 14:27:47
【问题描述】:

大家好,我需要帮助来删除此按钮并从 json 文件中加载数据,而无需单击该按钮 这是代码

List _items = [];

//从json文件中获取内容

@覆盖 小部件构建(BuildContext 上下文){ 未来的 readJson() 异步 { 最终字符串响应 = 等待 rootBundle.loadString('assets/aaaa.json'); 最终数据 = 等待 json.decode(response); 设置状态((){ _items = 数据['first']; }); }

return Scaffold(
  body: Padding(
    padding: const EdgeInsets.all(25),
    child: Column(
      children: [
        ElevatedButton(
          child: const Text('Load Data'),
          onPressed: readJson,
        ),
        // Display the data loaded from sample.json
        _items.isNotEmpty
            ? Expanded(
                child: ListView.builder(
                  itemCount: _items.length,
                  itemBuilder: (context, index) {
                    return Card(
                      margin: const EdgeInsets.all(10),
                      child: ListTile(
                        leading: Text(_items[index]["aaaaa"]),
                        title: Text(_items[index]["aaaaa"]),
                        subtitle: Text(_items[index]["aaaaaa"]),
                      ),
                    );
                  },
                ),
              )
            : Container()
      ],
    ),
  ),
);

}

【问题讨论】:

  • 在stackoverflow上发帖之前,请阅读您使用的工具的文档。

标签: flutter flutter-dependencies


【解决方案1】:

您应该查看Future Builder。该页面上有一些关于如何使用小部件的很好的示例,包括如何根据数据是否已加载、正在加载或出现错误来显示不同的小部件。 readJson 将是你的未来。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2022-10-16
    • 1970-01-01
    • 1970-01-01
    • 2018-01-27
    • 1970-01-01
    • 2014-12-30
    相关资源
    最近更新 更多