美文网首页
flutter 用json_serializable自动生成实体

flutter 用json_serializable自动生成实体

作者: 时光浅影 | 来源:发表于2022-09-22 15:41 被阅读0次

添加依赖

dev_dependencies:
  flutter_test:
    sdk: flutter
 
  build_runner: ^2.0.4
  json_serializable: ^4.1.3

新建文件xxx_bean.dart

import 'package:json_annotation/json_annotation.dart';
 
part 'student_bean.g.dart';//这里需要写上报错先不管 等输入命令生成文件后会自动刷新
@JsonSerializable()
class XXX{
  String? name;
 
  int? age;
 
  XXX({this.name, this.age});


  factory XXX.fromJson(Map<String, dynamic> json) => _$XXXFromJson(json);
 
  Map<String, dynamic> toJson() => _$XXXToJson(this);
}

pubspec.yaml路径下命令行输入

#生成命令
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

相关文章

网友评论

      本文标题:flutter 用json_serializable自动生成实体

      本文链接:https://www.haomeiwen.com/subject/kisnortx.html