Created
August 21, 2023 12:27
-
-
Save Daniel-Sogbey/ccf30fa35d0e9c483097f2aa02780f10 to your computer and use it in GitHub Desktop.
Cast JSON to Model instances
This file contains 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
import "dart:convert"; | |
void main() { | |
var rawJson = '{"url":"http://blah.jpg", "id":1}'; | |
var parsedJson = json.decode(rawJson); | |
var imageModel = ImageModel.fromJson(parsedJson); | |
} | |
class ImageModel { | |
String url; | |
int id; | |
ImageModel({required this.id, required this.url}); | |
ImageModel.fromJson(parsedJson) | |
: id = parsedJson['id'], | |
url = parsedJson['url']; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment