Skip to content

Instantly share code, notes, and snippets.

@AitoApps
Created August 17, 2019 20:02
Show Gist options
  • Save AitoApps/3aada97c0b61fa66a90aac4eca2351fb to your computer and use it in GitHub Desktop.
Save AitoApps/3aada97c0b61fa66a90aac4eca2351fb to your computer and use it in GitHub Desktop.
ParsingJSON-Flutter one-to-many relationship
class Product {
final int id;
final String name;
final List<Image> images;
Product({this.id, this.name, this.images});
factory Product.fromJson(Map<String, dynamic> parsedJson){
var list = parsedJson['images'] as List;
print(list.runtimeType);
List<Image> imagesList = list.map((i) => Image.fromJson(i)).toList();
return Product(
id: parsedJson['id'],
name: parsedJson['name'],
images: imagesList
);
}
}
class Image {
final int imageId;
final String imageName;
Image({this.imageId, this.imageName});
factory Image.fromJson(Map<String, dynamic> parsedJson){
return Image(
imageId:parsedJson['id'],
imageName:parsedJson['imageName']
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment