I have a json data like this format
{
"ID": 2,
"Name": "krish",
"info": [{
"Time": "07:30:00",
"Image": "https://www.ingredion.com/content/dam/ingredion/usca-images/food/meat/cheeseburger-bread_720x560.jpg",
"Notes": "Test"
}, {
"Time": "08:30:00",
"Image": "https://www.refrigeratedfrozenfood.com/ext/resources/NEW_RD_Website/DefaultImages/default-pasta.jpg",
"Notes": "Test1"
}, {
"Time": "09:30:00",
"Image": "https://images.unsplash.com/photo-1520342868574-5fa3804e551c?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=6ff92caffcdd63681a35134a6770ed3b&auto=format&fit=crop&w=1951&q=80",
"Notes": "Testw"
}
],
"status": 5,
"data": "passed"
}
how do we get all "Image" data only using flutter. thanks in advance.
CodePudding user response:
You could do
final responseMap = jsonDecode(responseString);
final infoList = responseMap['info'];
final imageList = infoList.map((info) => info['Image']).toList();
imageList will contain all the image links as List of String.
CodePudding user response:
Ok so from what i see the you can use the online tools to create the objects for parsing the data there is a tool online: quicktype.io which will make your task easy. I have just add the model from the tool iteself below check it.
// To parse this JSON data, do
//
// final user = userFromJson(jsonString);
import 'dart:convert';
User userFromJson(String str) => User.fromJson(json.decode(str));
String userToJson(User data) => json.encode(data.toJson());
class User {
User({
this.id,
this.name,
this.info,
this.status,
this.data,
});
int id;
String name;
List<Info> info;
int status;
String data;
factory User.fromJson(Map<String, dynamic> json) => User(
id: json["ID"],
name: json["Name"],
info: List<Info>.from(json["info"].map((x) => Info.fromJson(x))),
status: json["status"],
data: json["data"],
);
Map<String, dynamic> toJson() => {
"ID": id,
"Name": name,
"info": List<dynamic>.from(info.map((x) => x.toJson())),
"status": status,
"data": data,
};
}
class Info {
Info({
this.time,
this.image,
this.notes,
});
String time;
String image;
String notes;
factory Info.fromJson(Map<String, dynamic> json) => Info(
time: json["Time"],
image: json["Image"],
notes: json["Notes"],
);
Map<String, dynamic> toJson() => {
"Time": time,
"Image": image,
"Notes": notes,
};
}
For more advance stuff you can use a plugin that is available on flutter pub that is retrofit Link : https://pub.dev/packages/retrofit This will auto generate all the above stuff, you just have to follow some guide lines
Do let me know if it works.