Flutter json_serializable 不同引用键名切换请求

问题描述

我在问一个问题,但没有找到类似的帖子。

我解释一下,我使用的是json_serializable,但是webservice通过不同的名称键提供了一些相同类型对象的信息,例如:

@JsonSerializable(explicitToJson: true)
class Group {
  Group(
      this.id,this.name,this.owner,this.description);

  int id;
  String name;
  User owner; //Here owner are only "id" and "name"
  String? description;

...
}

@JsonSerializable()
class User {
  User(this.uid,this.nom,this.mail,this.prenom);

  String? uid;
  late String mail;
  String nom;
  String? prenom;

...

因此,“id”和“uid”是不同的键,但具有相同的值,“name”和“nom”的内容相同。

有什么方法可以向生成器表明对于相同的值,这可以是不同的键名..?

解决方法

我的解决方案是设置两个不同的变量,一个可以为NULL