This is my class:
@JsonSerializable()
class Foo {
final int a = 0;
final int b;
Foo(this.b);
factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
Map<String, dynamic> toJson() => _$FooToJson(this);
}
The generated code doesn't include a
field:
Map<String, dynamic> _$FooToJson(Foo instance) => <String, dynamic>{
'b': instance.b, // Only `b` is generated, `a` is missing.
};
What should I do to include a
in the generated code?
CodePudding user response:
part 'foo.g.dart';
@JsonSerializable()
class Foo {
int a = 0;
final int b;
Foo(this.a, this.b);
factory Foo.fromJson(Map<String, dynamic> json) => _$FooFromJson(json);
Map<String, dynamic> toJson() => _$FooToJson(this);
}
///////
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Foo _$FooFromJson(Map<String, dynamic> json) => Foo(
json['a'] as int,
json['b'] as int,
);
Map<String, dynamic> _$FooToJson(Foo instance) => <String, dynamic>{
'a': instance.a,
'b': instance.b,
};
CodePudding user response:
if you want easier json parsing, using Dart Data Class Generator extension in VS Code.