flutter 使用json_serializable自动生成m

2022-11-14  本文已影响0人  Liu_QT

使用json_serializable第三方库自动生成相应的实体类

  1. 添加依赖
dev_dependencies:
  flutter_test:
    sdk: flutter
 
  build_runner: ^2.0.4
  json_serializable: ^4.1.3
  1. 手动创建model类并写入相关属性

part 'brand_list_model.g.dart'; //这里报错没关系

@JsonSerializable()
class BrandListModel  {
  static const int pageSize = 10;

  int pageNo = 0;
  int totalCount = 0;

  @JsonKey(name: "data")
  List<BrandItemModel>? brandListModel = [];

  BrandListModel({
    this.pageNo = 0,
    this.totalCount = 0,
    this.brandListModel
  });
}
  1. 在对应的目录下输入命令
flutter packages pub run build_runner build

注意:遇到冲突Conflicting outputs were detected and the build is unable to prompt for permission to需要再输入两个命令

flutter packages pub run build_runner clean
flutter packages pub run build_runner build --delete-conflicting-outputs
上一篇下一篇

猜你喜欢

热点阅读