build_runner 没有生成正确的 toJson 方法
build_runner is not generating correct toJson methods
test.dart
文件:
import 'package:json_annotation/json_annotation.dart';
import 'foo.dart';
part 'test.g.dart';
@JsonSerializable()
class Test {
Test(this.foo);
factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);
Map<String, dynamic> toJson() => _$TestToJson(this);
final Foo? foo;
}
test.g.dart class
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Test _$TestFromJson(Map<String, dynamic> json) {
return Test(
json['foo'] == null
? null
: Foo.fromJson(json['foo'] as Map<String, dynamic>),
);
}
Map<String, dynamic> _$TestToJson(Test instance) => <String, dynamic>{
'foo': instance.foo,
};
几个月前,classes 是用另一个 _$TestToJson
生成的。每个自定义 class 字段都像这样:
'foo': instance.foo?.toJson(),
现在 ?.toJson()
不见了。结果是一个包含自定义 classes 对象的地图。但是我需要一棵带有地图地图的树.....因为当我尝试使用 fromJson 时,树不适合重新转换。
这是foo.dart
import 'package:json_annotation/json_annotation.dart';
part 'foo.g.dart';
@JsonSerializable()
class Foo {
Foo();
factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
Map<String, dynamic> toJson() => _$FooToJson(this);
}
缺少 build.yaml。
这是我的 build.yaml
targets:
$default:
builders:
json_serializable:
options:
explicit_to_json: true
explicit_to_json
默认为 false
explicitToJson
您可以像这样使用特定的 class 自定义它:
@JsonSerializable(explicitToJson: true)
class Test {
Test(this.foo);
factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);
Map<String, dynamic> toJson() => _$TestToJson(this);
final Foo? foo;
}
test.dart
文件:
import 'package:json_annotation/json_annotation.dart';
import 'foo.dart';
part 'test.g.dart';
@JsonSerializable()
class Test {
Test(this.foo);
factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);
Map<String, dynamic> toJson() => _$TestToJson(this);
final Foo? foo;
}
test.g.dart class
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'test.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Test _$TestFromJson(Map<String, dynamic> json) {
return Test(
json['foo'] == null
? null
: Foo.fromJson(json['foo'] as Map<String, dynamic>),
);
}
Map<String, dynamic> _$TestToJson(Test instance) => <String, dynamic>{
'foo': instance.foo,
};
几个月前,classes 是用另一个 _$TestToJson
生成的。每个自定义 class 字段都像这样:
'foo': instance.foo?.toJson(),
现在 ?.toJson()
不见了。结果是一个包含自定义 classes 对象的地图。但是我需要一棵带有地图地图的树.....因为当我尝试使用 fromJson 时,树不适合重新转换。
这是foo.dart
import 'package:json_annotation/json_annotation.dart';
part 'foo.g.dart';
@JsonSerializable()
class Foo {
Foo();
factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
Map<String, dynamic> toJson() => _$FooToJson(this);
}
缺少 build.yaml。
这是我的 build.yaml
targets:
$default:
builders:
json_serializable:
options:
explicit_to_json: true
explicit_to_json
默认为 false
explicitToJson
您可以像这样使用特定的 class 自定义它:
@JsonSerializable(explicitToJson: true)
class Test {
Test(this.foo);
factory Test.fromJson(Map<String, dynamic> json) => _$TestFromJson(json);
Map<String, dynamic> toJson() => _$TestToJson(this);
final Foo? foo;
}