How to call nested json data using API in flutter?

710

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

Share:
710
parveen
Author by

parveen

Updated on December 31, 2022

Comments

  • parveen
    parveen over 1 year

    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!

    • Dinesh Nagarajan
      Dinesh Nagarajan over 2 years
    • Ravindra S. Patil
      Ravindra S. Patil over 2 years
      If you get data From API refer my answer here or here
    • Dinesh Nagarajan
      Dinesh Nagarajan over 2 years
      also the json data you provided is not a valid JSON.
    • parveen
      parveen over 2 years
      @Ravindra S. Patil, I tried both but I didn't get output. Now, this error is showing "error: The operator '[]' isn't defined for the type 'Album'"
    • Ravindra S. Patil
      Ravindra S. Patil over 2 years
      can you tell me your json data is host online? because my second answer json data and your json data is look like same
    • parveen
      parveen over 2 years
      yes, I have copy-pasted the JSON data here on above and I have followed 2nd answer. @Ravindra S. Patil
    • Ravindra S. Patil
      Ravindra S. Patil over 2 years
      Can you share your json url I try to solve your problem
    • Ravindra S. Patil
      Ravindra S. Patil over 2 years
      @parveen check my answer your problem is solved
  • parveen
    parveen over 2 years
    and what if i want to show all the list data?
  • Kim Lo
    Kim Lo almost 2 years
    hi sir ravindra. can i comment here a follow-up question related to this problem? thanks
  • Ravindra S. Patil
    Ravindra S. Patil almost 2 years
    @KimLo Yes sure, say your problem
  • Kim Lo
    Kim Lo almost 2 years
    what if i have a similar list, but this time it is nested. How do i make each textfield of the sublist unique? bcoz in my program now, whenever i input 55 on the textfield, it will change all the textfield on the sublist with the same index. thanks! see image link. imgur.com/IjOqWPJ