将Flutter Freeze字段名称重命名为snake、pascal、kebab或anothing



发布这个问题是为了自己回答。希望这能帮助到别人。

问题:使用flutterfreezed生成代码,但将to和from json字段名更改为特定的重命名模式。

解决方案:

build.yaml

假设你有一个用freezed写的类,就像这个

import 'package:freezed_annotation/freezed_annotation.dart';
import 'dart:convert';
part 'my_calss.freezed.dart';
part 'my_calss.g.dart';
@freezed
abstract class MyCalss with _$MyCalss {
const factory MyCalss({
@required int field1,
@required String field2,
}) = _MyCalss;
factory MyCalss.fromJson(Map<String, dynamic> json) => _$MyCalssFromJson(json);
}

更改类别my_calss.g.dart以使用例如pascal情况:

将以下内容放入build.yaml

# targets:
#   $default:
#     builders:
#       freezed:
#         options:
#           union_key: type
#           union_value_case: pascal
targets:
$default:
builders:
json_serializable:
options:
# Options configure how source code is generated for every
# `@JsonSerializable`-annotated class in the package.
#
# The default value for each is listed.
any_map: false
checked: false
constructor: ""
create_factory: true
create_field_map: false
create_to_json: true
disallow_unrecognized_keys: false
explicit_to_json: false
field_rename: pascal
generic_argument_factories: false
ignore_unannotated: false
include_if_null: true

感兴趣的主要字段是field_rename,它可以是下面列出的json_serializable中枚举FieldRename的任何值


/// Values for the automatic field renaming behavior for [JsonSerializable].
enum FieldRename {
/// Use the field name without changes.
none,
/// Encodes a field named `kebabCase` with a JSON key `kebab-case`.
kebab,
/// Encodes a field named `snakeCase` with a JSON key `snake_case`.
snake,
/// Encodes a field named `pascalCase` with a JSON key `PascalCase`.
pascal,
/// Encodes a field named `screamingSnakeCase` with a JSON key
/// `SCREAMING_SNAKE_CASE`
screamingSnake,
}

最初,我在这里找到了这个github页面,上面写着要做以下

However, when I write this, there is an error (when I checked the generated file, it was generated twice)
@freezed
@JsonSerializable(fieldRename: FieldRename.snake)
class Example with _$Example {
factory Example() = GeneratedClass;
}
So rewriting it this way works fine.
@freezed
class Example with _$Example {
@JsonSerializable(fieldRename: FieldRename.snake)
factory Example() = GeneratedClass;
}

这两种解决方案都没有帮助我,只是编辑了build.yaml文件解决了这个问题。

我使用的版本

#Freezed 
freezed_annotation: ^2.1.0
#json annotations
json_annotation: ^4.6.0
dev_dependencies:
flutter_test:
sdk: flutter
build_runner: ^2.2.0
flutter_lints: ^2.0.1
freezed: ^2.1.0+1
json_serializable: ^6.3.1

实际上,我找到了一种更简单的方法。它可能是新的,但也有一些文档记录了如何将冷冻与JsonSerializable表示法混合使用。

https://pub.dev/packages/freezed(查找"@JsonSerializable annotation怎么样?"部分(

这种代码对我来说就像一种魅力。

@freezed
class Account with _$Account {
// ignore: invalid_annotation_target
@JsonSerializable(fieldRename: FieldRename.snake)
const factory Account(
{required int? id,
required int? userId, // snake case to camel case conversion works
required String? name,
required int? balance}) = _Account;
factory Account.fromJson(Map<String, dynamic> json) =>
_$AccountFromJson(json);
}

您还可以使用@JsonKey(名称:'field_rename'(指定json密钥

@freezed
abstract class MyCalss with _$MyCalss {
const factory MyCalss({
@JsonKey(name: 'field_name') @required int fieldName,
@JsonKey(name: 'field_age') @required String fieldAge,
@required String gender,
}) = _MyCalss;
factory MyCalss.fromJson(Map<String, dynamic> json) => _$MyCalssFromJson(json);
}

相关内容

  • 没有找到相关文章

最新更新