颤音:无法将嵌套模型与实体使用。 (实体,模型,平等,颤抖,飞镖,JSON_SERIALIZABLE,JSON序列化)
我有两个文件用于单个数据IE实体和模型。将整个逻辑彼此分开并遵循最佳实践。 每当我试图使用嵌套实体并通过@jsonerializable进行解析时,它会显示出错误。
注意:我也尝试过@jsonConverter。它不起作用。
我敢肯定,我在那儿错过了一些东西,但无法理解。 请帮助。
test_entity.dart
class TestEntity extends Equatable {
final String id;
final List<AddressEntity> address;
const TestEntity({
required this.id,
required this.address,
});
@override
List<Object?> get props => [
id,
address,
];
}
test_model.dart
part 'test_model.g.dart';
@JsonSerializable(explicitToJson: true)
class TestModel extends TestEntity {
const TestModel({
required String id,
required List<AddressModel> address,
}) : super(
id: id,
address: address,
);
factory TestModel.fromJson(Map<String, dynamic> json) =>
_$TestModelFromJson(json);
Map<String, dynamic> toJson() => _$TestModelToJson(this);
}
address_entity.dart
class AddressEntity extends Equatable {
final String id;
final String title;
const AddressEntity({
required this.id,
required this.title,
});
@override
List<Object?> get props => [
id,
title,
];
}
address_model.dart
part 'address_model.g.dart';
@JsonSerializable()
class AddressModel {
final String id;
final String title;
AddressModel({
required this.id,
required this.title,
});
factory AddressModel.fromJson(Map<String, dynamic> json) =>
_$AddressModelFromJson(json);
Map<String, dynamic> toJson() => _$AddressModelToJson(this);
}
当我使用addressModel而代替地址
I have two file for a single data i.e Entity and Model. To separate the entire logic from each other and to follow best practices.
Whenever I am trying to use nested Entities and parsing through @JsonSerializable it's showing me error.
Note: I have tried @JsonConverter too. It's not working.
I'm sure I'm missing something over there but can't get it.
Please help.
test_entity.dart
class TestEntity extends Equatable {
final String id;
final List<AddressEntity> address;
const TestEntity({
required this.id,
required this.address,
});
@override
List<Object?> get props => [
id,
address,
];
}
test_model.dart
part 'test_model.g.dart';
@JsonSerializable(explicitToJson: true)
class TestModel extends TestEntity {
const TestModel({
required String id,
required List<AddressModel> address,
}) : super(
id: id,
address: address,
);
factory TestModel.fromJson(Map<String, dynamic> json) =>
_$TestModelFromJson(json);
Map<String, dynamic> toJson() => _$TestModelToJson(this);
}
address_entity.dart
class AddressEntity extends Equatable {
final String id;
final String title;
const AddressEntity({
required this.id,
required this.title,
});
@override
List<Object?> get props => [
id,
title,
];
}
address_model.dart
part 'address_model.g.dart';
@JsonSerializable()
class AddressModel {
final String id;
final String title;
AddressModel({
required this.id,
required this.title,
});
factory AddressModel.fromJson(Map<String, dynamic> json) =>
_$AddressModelFromJson(json);
Map<String, dynamic> toJson() => _$AddressModelToJson(this);
}
When I use AddressModel instead AddressEntity
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Jsonserializeble类的任何属性都应具有自己的
FromJSON()
构造函数。您的TestEntity类具有列表&lt; equebentity&gt;
作为属性,但是peadmentity
没有此类构造函数。要解决此问题,只需实现
fromjson()
构造函数和tojson()
empectentity
类的方法。如果您手工或借助Jsonserializeble Generator制作没有区别。Any property of a JsonSerializeble class should have its own
fromJson()
constructor. Your TestEntity class has theList<AddressEntity>
as a property, butAddressEntity
has no such constructor.To fix this, just implement
fromJson()
constructor andtoJson()
method for theAddressEntity
class. There is no difference if you make it by hand or with the help of the JsonSerializeble generator.