Dart 忽略属性生成运行程序序列化的标志
有没有办法忽略JsonSerializable类中属性的序列化 我正在使用build_runner生成映射代码 实现这一点的一种方法是在.g.dart-file中注释该特定属性的映射,不过如果可以在该属性上添加一个ignore属性就更好了Dart 忽略属性生成运行程序序列化的标志,dart,flutter,json-serialization,Dart,Flutter,Json Serialization,有没有办法忽略JsonSerializable类中属性的序列化 我正在使用build_runner生成映射代码 实现这一点的一种方法是在.g.dart-file中注释该特定属性的映射,不过如果可以在该属性上添加一个ignore属性就更好了 import 'package:json_annotation/json_annotation.dart'; part 'example.g.dart'; @JsonSerializable() class Example { Example({thi
import 'package:json_annotation/json_annotation.dart';
part 'example.g.dart';
@JsonSerializable()
class Example {
Example({this.a, this.b, this.c,});
int a;
int b;
/// Ignore this property
int c;
factory Example.fromJson(Map<String, dynamic> json) =>
_$ExampleFromJson(json);
Map<String, dynamic> toJson() => _$ExampleToJson(this);
}
import'package:json_annotation/json_annotation.dart';
“示例g.省道”部分;
@JsonSerializable()
课例{
示例({this.a,this.b,this.c,});
INTA;
int b;
///忽略此属性
INTC;
工厂示例.fromJson(映射json)=>
_$ExampleFromJson(json);
映射到JSON()=>$ExampleToJson(此);
}
导致
Example _$ExampleFromJson(Map<String, dynamic> json) {
return Example(a: json['a'] as int, b: json['b'] as int, c: json['c'] as int);
}
Map<String, dynamic> _$ExampleToJson(Example instance) =>
<String, dynamic>{'a': instance.a, 'b': instance.b, 'c': instance.c};
Example\u$ExampleFromJson(映射json){
返回示例(a:json['a']作为int,b:json['b']作为int,c:json['c']作为int);
}
映射$ExampleToJson(示例实例)=>
{'a':instance.a,'b':instance.b,'c':instance.c};
我所做的是通过注释c的映射来实现这一点
Example _$ExampleFromJson(Map<String, dynamic> json) {
return Example(a: json['a'] as int, b: json['b'] as int, c: json['c'] as int);
}
Map<String, dynamic> _$ExampleToJson(Example instance) =>
<String, dynamic>{'a': instance.a, 'b': instance.b, /* 'c': instance.c */};
Example\u$ExampleFromJson(映射json){
返回示例(a:json['a']作为int,b:json['b']作为int,c:json['c']作为int);
}
映射$ExampleToJson(示例实例)=>
{'a':instance.a,'b':instance.b,/*'c':instance.c*/};
在不希望包含的字段之前添加@JsonKey(忽略:true)
@JsonKey(ignore: true)
int c;
另请参见太棒了,它就像一个符咒!我只是查看了build_runner文档,在那里找不到它。我猜它在JsonAnnotation文档中?感谢您的快速响应。当您使用继承时,这似乎不起作用…如何使它只忽略
toJson
方法?请原谅我之前的语言,我的用例是:有property
我想从json转换到我的模型,但我想在从模型创建json时忽略,希望这个答复能澄清这一点。@buncis我想我明白了,但我没有解决办法