Created
August 17, 2019 20:02
-
-
Save AitoApps/3aada97c0b61fa66a90aac4eca2351fb to your computer and use it in GitHub Desktop.
ParsingJSON-Flutter one-to-many relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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