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文件中添加如下內(nèi)容
analyzer:
errors:
invalid_annotation_target: ignore
vscode 安裝插件
flutter freezed helpres
然后通過創(chuàng)建一個user_model.dart的文件
然后通過 frf 快捷方式,快速創(chuàng)建一個類偿衰、
通過 frc 也可以快速生成對象蓝丙,只是這里生成的_$toJson(this)是多余的敏释,可以直接刪除氯夷。因為默認已經(jīng)支持toJson方法了。
freezed生成的代碼雷客,已經(jīng)內(nèi)置了copyWIth方法裙秋。