Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
112 views
in Technique[技术] by (71.8m points)

How to call nested json data using API in flutter?

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'.

enter image description here

Hoping for help!

Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 -> enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...