安装依赖
flutter packages add build_runner
flutter packages add json_serializable
定义文件
- lib/models/user.dart
import 'package:json_annotation/json_annotation.dart';
part 'user.g.dart';
@JsonSerializable()
class User{
User(this.name, this.email);
String name;
String email;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
Map<String, dynamic> toJson() => _$UserToJson(this);
}
生成模型文件
flutter packages pub run build_runner build
标签:packages,模型,生成,json,User,dart,flutter,build
From: https://www.cnblogs.com/guanchaoguo/p/17799924.html