A new Flutter project.
git clone [this one]
flutter get
-
add annotation on class
@JsonSerializable() class User { final int id; final String name; final String email; // ... }
-
pubspec.yaml
, add packagejson_serializable
dependencies: # ...others json_serializable: any
-
then fetch packages
flutter pub get
-
add package
build_runner
todev_dependencies
dependencies: # ...others json_serializable: any dev_dependencies: # ...others build_runner:
-
then do this, it will generate a
user.g.dart
for youflutter pub run build_runner build
-
add methods
part 'user.g.dart'; @JsonSerializable() class User { factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json); Map<String, dynamic> toJson() => _$UserToJson(this);
-
Now you don't need to extract and assign properties one by one from the JSON.