如何在flutter中使用API​​调用嵌套的json数据? [英] How to call nested json data using API in flutter?

查看:24
本文介绍了如何在flutter中使用API​​调用嵌套的json数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 JSON 如下所示:

<代码>{信息:[{c_type_id:1",清洁类型:A清洁"},{c_type_id:2",清洁类型:B清洁"},{c_type_id:3",清洁类型:C清洁"},{c_type_id:4",清洁类型:D清洁"},{c_type_id:5",清洁类型:E清洁"},]}

这是代码:以下代码由

希望得到帮助!

解决方案

你应该试试下面的代码你的问题已经解决了 ->

声明您的 API 调用函数

未来<列表<动态>>getInfoData() 异步 {String url = 'https://fillmmaka.com/gigocleanapi/cleanintypes.php';var response = await http.get(Uri.parse(url), headers: {'内容类型':'应用程序/json','接受':'应用程序/json',});返回 json.decode(response.body)['Info'];}

声明您的小部件

中心(孩子:FutureBuilder>(未来:getInfoData(),构建器:(上下文,快照){如果(快照.hasData){返回填充(填充:const EdgeInsets.all(8.0),孩子:ListView.builder(itemCount:snapshot.data.length,itemBuilder:(上下文,索引){var id = snapshot.data[index]['c_type_id'];var type = snapshot.data[index]['cleaning type'];退货卡(形状:RoundedRectangleBorder(边:边界边(颜色:Colors.green.shade300,),borderRadius: BorderRadius.circular(15.0),),孩子:ListTile(领先:文本(ID),标题:文本(类型),),);},),);}返回 CircularProgressIndicator();},),),

你的屏幕看起来像这样 ->

My JSON looks like this:

{
Info: [
       {
         c_type_id: "1",
         cleaning type: "A Cleaning"
        },
        {
          c_type_id: "2",
          cleaning type: "B Cleaning"
         },
         {
           c_type_id: "3",
           cleaning type: "C Cleaning"
         },
         {
           c_type_id: "4",
           cleaning type: "D Cleaning"
          },
          {
            c_type_id: "5",
            cleaning type: "E Cleaning"
           },
       ]
}

and here is the code: The following code is created by this Class 1:

class Album {
   List<Info> info;
   Album({this.info})
     Album.fromJson(Map<String, dynamic> json) {
      if (json['Info'] != null) {
       info =  List<Info>.empty();
       json['Info'].forEach((v) {
       info.add(new Info.fromJson(v));
      });
     }
    }

   Map<String, dynamic> toJson() {
     final Map<String, dynamic> data = new Map<String, dynamic>();
      if (this.info != null) {
      data['Info'] = this.info.map((v) => v.toJson()).toList();
      }
     return data;
     }
    }

class 2:

class Info {
  String cTypeId;
  String cleaningType;
  Info({this.cTypeId, this.cleaningType});
    Info.fromJson(Map<String, dynamic> json) {
    cTypeId = json['c_type_id'];
    cleaningType = json['cleaning type'];
    }
   Map<String, dynamic> toJson() {
   final Map<String, dynamic> data = new Map<String, dynamic>();
   data['c_type_id'] = this.cTypeId;
   data['cleaning type'] = this.cleaningType;
   return data;
 }
}

This is the error I get when I execute the code: error: The argument type 'List' can't be assigned to the parameter type 'String'.

Hoping for help!

解决方案

You should Try below code your problem has been solved ->

Declare your API Call funtion

Future<List<dynamic>> getInfoData() async {
    String url = 'https://fillmmaka.com/gigocleanapi/cleanintypes.php';
    var response = await http.get(Uri.parse(url), headers: {
      'Content-Type': 'application/json',
      'Accept': 'application/json',
    });
    return json.decode(response.body)['Info'];
  }

Declare your Widget

Center(
    child: FutureBuilder<List<dynamic>>(
      future: getInfoData(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Padding(
            padding: const EdgeInsets.all(8.0),
            child: ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (context, index) {
                var id = snapshot.data[index]['c_type_id'];
                var type = snapshot.data[index]['cleaning type'];

                return Card(
                  shape: RoundedRectangleBorder(
                    side: BorderSide(
                      color: Colors.green.shade300,
                    ),
                    borderRadius: BorderRadius.circular(15.0),
                  ),
                  child: ListTile(
                    leading: Text(id),
                    title: Text(type),
                  ),
                );
              },
            ),
          );
        }
        return CircularProgressIndicator();
      },
    ),
  ),

Your Screen look like this ->

这篇关于如何在flutter中使用API​​调用嵌套的json数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆