Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在flatter中将json解析为list而不是map_Json_Flutter - Fatal编程技术网

如何在flatter中将json解析为list而不是map

如何在flatter中将json解析为list而不是map,json,flutter,Json,Flutter,如何解析JSON以在flatter中列出。我已经使用了联机解析Json,但该工具正在将其转换为映射。我需要在列表中获取解析后的json,并在页面的listview生成器中返回内容列表。下面是我的json文件和解决方法 Json 赫伯斯特省 class Herbs extends StatefulWidget { final String title; Herbs(this.title); @override _HerbsState createState() => new

如何解析JSON以在flatter中列出。我已经使用了联机解析Json,但该工具正在将其转换为映射。我需要在列表中获取解析后的json,并在页面的listview生成器中返回内容列表。下面是我的json文件和解决方法

Json

赫伯斯特省

class Herbs extends StatefulWidget {
  final String title;
  Herbs(this.title);

  @override
  _HerbsState createState() => new _HerbsState();
}

class _HerbsState extends State<Herbs> {
  var cname;

  Future<Herbslist> fetchPost() async {
    final response = await http.get(
        'https://example.org/api.php?action=query&gcmtitle=Category:$cname&pilimit=max&prop=pageimages&pithumbsize=600&generator=categorymembers&format=json&gcmcontinue='
        );
    if (response.statusCode == 200) {
      if(this.mounted){
      return Herbslist.fromJson(json.decode(response.body));
      }
    } else {
      print(Exception);
      throw (e) {
        print("Exception thrown: $e");
        Exception(e);
      };
    }
  }

  @override
  Widget build(BuildContext context) {
    cname = widget.title;
    return new Scaffold(
        appBar: AppBar(
          title: Align(
            alignment: Alignment(-0.2, 0.3),
            child: Text(
              cname,
            ),
          ),
        ),
        body: Center(
          child: FutureBuilder<Herbslist>(
            future: fetchPost(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return ListView.builder(
                  shrinkWrap: true,
                  itemCount: snapshot.data.query.pages.length,
                  itemBuilder: (BuildContext context, int index) {
                  //  var gcm = snapshot.data.herbslistContinue.gcmcontinue;
                    var img = snapshot.data.query.pages.values
                        .toList()[index]
                        .thumbnail
                        .source;
                    return Container(
                        child: Card(
                            child: GestureDetector(
                      onTap: () {
                        Navigator.push(
                            context,
                            MaterialPageRoute(
                              builder: (context) => Detailpage(
                                snapshot.data.query.pages.values
                                    .toList()[index]
                                    .title,
                              ),
                            ));
                      },
                      child: ListTile(
                        contentPadding: EdgeInsets.symmetric(
                            horizontal: 8.0, vertical: 8.0),
                        leading: Container(
                            padding: EdgeInsets.only(right: 10.0),
                            decoration: new BoxDecoration(
                              border: new Border(
                                  right: new BorderSide(
                                      width: 1.5, color: Colors.grey)),
                            ),
                            // ignore: unrelated_type_equality_checks
                            child: img == img.isEmpty
                                ? SizedBox(
                                    height: 50.0,
                                    width: 50.0,
                                    child: Image.asset('image.png'),
                                  )
                                : SizedBox(
                                    height: 50.0,
                                    width: 50.0,
                                    child: FadeInImage.assetNetwork(
                                      placeholder: 'image.png',
                                      image: img,
                                      fit: BoxFit.fill,
                                    ),
                                  )),
                        title: Text(snapshot.data.query.pages.values
                            .toList()[index]
                            .title),
                      ),
                    )));
                  },
                );
              } else {
                return Center(
                  child: CircularProgressIndicator(),
                );
              }
            },
          ),
        ));
  }
}


  [1]: https://app.quicktype.io/
类扩展StatefulWidget{
最后的字符串标题;
草药(本标题);
@凌驾
_HerbsState createState()=>new_Herbstate();
}
类_herbstate扩展状态{
var-cname;
Future fetchPost()异步{
最终响应=等待http.get(
'https://example.org/api.php?action=query&gcmtitle=Category:$cname&pilimit=max&prop=pageimages&pitumbsize=600&generator=categorymembers&format=json&gcmcontinue=
);
如果(response.statusCode==200){
如果(本安装){
返回Herbslist.fromJson(json.decode(response.body));
}
}否则{
打印(例外);
投掷(e){
打印(“抛出异常:$e”);
例外情况(e);
};
}
}
@凌驾
小部件构建(构建上下文){
cname=widget.title;
归还新脚手架(
appBar:appBar(
标题:对齐(
对齐:对齐(-0.2,0.3),
子:文本(
cname,
),
),
),
正文:中(
孩子:未来建设者(
future:fetchPost(),
生成器:(上下文,快照){
if(snapshot.hasData){
返回ListView.builder(
收缩膜:对,
itemCount:snapshot.data.query.pages.length,
itemBuilder:(构建上下文,int索引){
//var gcm=snapshot.data.herbslitcontinue.gcmcontinue;
var img=snapshot.data.query.pages.values
.toList()[索引]
.缩略图
.来源;
返回容器(
孩子:卡片(
儿童:手势检测器(
onTap:(){
导航器。推(
上下文
材料路线(
生成器:(上下文)=>Detailpage(
snapshot.data.query.pages.values
.toList()[索引]
标题
),
));
},
孩子:ListTile(
contentPadding:EdgeInsets.symmetric(
水平:8.0,垂直:8.0),
领先:集装箱(
填充:仅限边缘设置(右侧:10.0),
装饰:新盒子装饰(
边界:新边界(
右:新边界(
宽度:1.5,颜色:颜色。灰色),
),
//忽略:不相关的\u类型\u相等\u检查
child:img==img.isEmpty
?尺寸箱(
身高:50.0,
宽度:50.0,
子项:Image.asset('Image.png'),
)
:SizedBox(
身高:50.0,
宽度:50.0,
子项:FadeInImage.assetNetwork(
占位符:“image.png”,
图片:img,
fit:BoxFit.fill,
),
)),
标题:文本(snapshot.data.query.pages.values)
.toList()[索引]
(标题),,
),
)));
},
);
}否则{
返回中心(
子对象:CircularProgressIndicator(),
);
}
},
),
));
}
}
[1]: https://app.quicktype.io/

假设变量
jsonMap
就是上面显示的json,您就可以做这么简单的事情:

jsonMap['query']['pages'].values.toList()
编辑:编辑以更改为评论中建议的更好的解决方案

下面是如何将其应用于代码的示例:

FutureBuilder<Herbslist>(
  future: fetchPost(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      List pages = snapshot.data['query']['pages'].values.toList();
      return ListView.builder(
        shrinkWrap: true,
        itemCount: pages.length,
FutureBuilder(
future:fetchPost(),
生成器:(上下文,快照){
if(snapshot.hasData){
List pages=snapshot.data['query']['pages'].values.toList();
返回ListView.builder(
收缩膜:对,
itemCount:pages.length,

确保更改其余代码以使用新的
页面
变量。

您的问题和书面英语非常混乱。请查看问题指南:我已经更新了我的问题,您现在可以获取我的英语@Joãosares您希望将json的哪一部分转换为列表吗?json获取conv是正常的我希望查询部分作为list@Joãosaares使用
jsonMap['query']['pages'].values.toList()不是更简洁吗
?@Mahant已经在这样做了。这可以通过
fetchPost
方法来完成,因此只需要做一次。目前它被做了N*3次。是的,你完全正确。我最初非常专注于使用.map,最终得到了一个更复杂的解决方案。你能更详细地解释一下我吗。我很困惑ed where to implementation@Joãosaares我在回复中添加了一个示例,其中包含了您的代码
FutureBuilder<Herbslist>(
  future: fetchPost(),
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      List pages = snapshot.data['query']['pages'].values.toList();
      return ListView.builder(
        shrinkWrap: true,
        itemCount: pages.length,