Skip to content

Instantly share code, notes, and snippets.

@Daniel-Sogbey
Created August 21, 2023 12:27
Show Gist options
  • Save Daniel-Sogbey/ccf30fa35d0e9c483097f2aa02780f10 to your computer and use it in GitHub Desktop.
Save Daniel-Sogbey/ccf30fa35d0e9c483097f2aa02780f10 to your computer and use it in GitHub Desktop.
Cast JSON to Model instances
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