JSON | Flutter
- JSON | Flutter
Artigo
JsonDecode
// 1. import dart:convert
import 'dart:convert';
// this represents some response data we get from the network
final jsonData = '{ "name": "Pizza da Mario", "cuisine": "Italian" }';
// 2. decode the json
final parsedJson = jsonDecode(jsonData);
// 3. print the type and value
print('${parsedJson.runtimeType} : $parsedJson');
Json to Dart
factory Restaurant.fromJson(Map<String, dynamic> data) {
// ! there's a problem with this code (see below)
final name = data['name'];
final cuisine = data['cuisine'];
return Restaurant(name: name, cuisine: cuisine);
}