freezed 使用教程
教程链接:https://pub.dev/packages/freezed
安装:
flutter pub add freezed_annotation
flutter pub add dev:build_runner
flutter pub add dev:freezed
# if using freezed to generate fromJson/toJson, also add:
flutter pub add json_annotation
flutter pub add dev:json_serializable
Disabling invalid_annotation_target warning and warning in generates files
If you plan on using Freezed in combination with json_serializable, recent versions of json_serializable and meta may require you to disable the invalid_annotation_target warning.
To do that, you can add the following to the analysis_options.yaml file at the root of your project:
即在根目录的analysis_options.yaml文件中添加如下内容
analyzer:
errors:
invalid_annotation_target: ignore
vscode 安装插件
flutter freezed helpres
然后通过创建一个user_model.dart的文件
然后通过 frf 快捷方式,快速创建一个类、
通过 frc 也可以快速生成对象,只是这里生成的_$toJson(this)是多余的,可以直接删除。因为默认已经支持toJson方法了。
freezed生成的代码,已经内置了copyWIth方法。