繁体   English   中英

flutter中key值数据如何显示本地json文件?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM