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();
},
),
),
Author by
parveen
Updated on December 31, 2022Comments
-
parveen 5 months
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!
-
Dinesh Nagarajan almost 2 years
-
Ravindra S. Patil almost 2 years
-
Dinesh Nagarajan almost 2 yearsalso the
json
data you provided is not a validJSON
. -
parveen almost 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 almost 2 yearscan you tell me your json data is host online? because my second answer json data and your json data is look like same
-
parveen almost 2 yearsyes, I have copy-pasted the JSON data here on above and I have followed 2nd answer. @Ravindra S. Patil
-
Ravindra S. Patil almost 2 yearsCan you share your json url I try to solve your problem
-
Ravindra S. Patil almost 2 years@parveen check my answer your problem is solved
-
-
parveen almost 2 yearsand what if i want to show all the list data?
-
Kim Lo about 1 yearhi sir ravindra. can i comment here a follow-up question related to this problem? thanks
-
Ravindra S. Patil about 1 year@KimLo Yes sure, say your problem
-
Kim Lo about 1 yearwhat 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