【发布时间】: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