颤音:无法将嵌套模型与实体使用。 (实体,模型,平等,颤抖,飞镖,JSON_SERIALIZABLE,JSON序列化)

发布于 2025-02-08 10:25:51 字数 1847 浏览 4 评论 0原文

我有两个文件用于单个数据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

Here is the Snap of Code

Also This Code:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

年华零落成诗 2025-02-15 10:25:51

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 the List<AddressEntity> as a property, but AddressEntity has no such constructor.

To fix this, just implement fromJson() constructor and toJson() method for the AddressEntity class. There is no difference if you make it by hand or with the help of the JsonSerializeble generator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文