flutter 快速生成模型文件

安装依赖

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 
posted @ 2023-10-31 11:43  vx_guanchaoguo0  阅读(47)  评论(0编辑  收藏  举报