[英]How to display local json file with key value data in flutter?
我有這個 json 文件,我只想在 ListView 小部件中顯示此數據的鍵。 像照片中的東西。 我是 flutter 的新人。
{
"cards": [
{
"name": {
"Title Card 1": 3,
"Title Card 2": 1,
"Title Card 3": 1,
"Title Card 4": 2,
"Title Card 5": 1
},
"authors": {
"Author John": 2,
"Author Carl": 1,
"Author Ben": 5
},
"language": {
"en": 74,
"fr": 11
}
}
]
}
以這種方式嘗試...
// Fetch content from the json file
Future<void> readJson() async {
final String response = await rootBundle.loadString('assets/sample.json');
final data = await json.decode(response);
setState(() {
_items = data["items"];
});
}
並以這種方式顯示
_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]["id"]),
title: Text(_items[index]["name"]),
subtitle: Text(_items[index]["description"]),
),
);
},
),
)
: Container()
有關更多信息,請查看此鏈接: Json 數據顯示
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.