首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >颤振json_serializable构建失败

颤振json_serializable构建失败
EN

Stack Overflow用户
提问于 2019-11-18 12:25:44
回答 2查看 7.4K关注 0票数 6

我正在使用json_serializablejson_annotation和build为我的模型生成serialization/deserialization功能。但是,当我运行构建时,我会得到这个错误。

运行

错误JsonSerializableGenerator无法填充所需的构造函数参数:已创建。package:explorer/models/Account/account.dart:46:3

它所指的行是我的模型构造函数,即。

代码语言:javascript
运行
复制
Account(String id, String firstName, String lastName, String email,
  DateTime dob, DateTime created, DateTime updated,
  {String accessTkn, String refreshTkn}) {}

我为什么要犯这个错误?

按照要求,这是我的模型类。

代码语言:javascript
运行
复制
import "package:json_annotation/json_annotation.dart";

part "account.g.dart";

@JsonSerializable(nullable: true)
class Account {
  @JsonKey(name: "id")
  String _id;

  @JsonKey(name: "first_name")
  String _firstName;

  @JsonKey(name: "last_name")
  String _lastName;

  @JsonKey(name: "email")
  String _email;

  @JsonKey(
      name: "dob", fromJson: _isoStringToDateTime, toJson: _dateTimeToIsoString)
  DateTime _dob;

  @JsonKey(
      name: "created",
      fromJson: _isoStringToDateTime,
      toJson: _dateTimeToIsoString)
  DateTime _created;

  @JsonKey(
      name: "updated",
      fromJson: _isoStringToDateTime,
      toJson: _dateTimeToIsoString)
  DateTime _updated;

  @JsonKey(name: "access_token")
  String _accessToken;

  @JsonKey(name: "refresh_token")
  String _refreshToken;

  Account(String id, String firstName, String lastName, String email,
      DateTime dob, DateTime created, DateTime updated,
      {String accessTkn, String refreshTkn}) {
    this._id = id;
    this._firstName = firstName;
    this._lastName = lastName;
    this._email = email;
    this._dob = dob;
    this._created = created;
    this._updated = updated;
    this._accessToken = accessToken;
    this._refreshToken = refreshTkn;
  }

  factory Account.fromJson(Map<String, dynamic> json) {
    _$AccountFromJson(json);
  }

  // converts a DateTime to a ISO string
  static String _dateTimeToIsoString(DateTime date) {
    return date.toIso8601String();
  }

  // convert back to date time
  static DateTime _isoStringToDateTime(String iso) {
    return DateTime.parse(iso);
  }

  /// get the account id
  String get id {
    return this._id;
  }

  /// get the account first name
  String get firstName {
    return this._firstName;
  }

  /// get the account last name
  String get lastName {
    return this._lastName;
  }

  /// get the account email.
  String get email {
    return this._email;
  }

  /// get the account owner's date of birth
  DateTime get dob {
   return this._dob;
  }

  /// Get the date the account was created.
  DateTime get createdAt {
    return this._created;
  }

  /// get teh date the account was last updated.
  DateTime get updatedAt {
   return this._updated;
  }

  // get the account access token.
  String get accessToken {
    return this._accessToken;
  }

  // get the account refresh token.
  String get refreshToken {
    return this._refreshToken;
  }

  /// clones the account instance
  Account clone() {
    return Account(this.id, this.firstName, this.lastName, this.email, this.dob,
        this.createdAt, this.updatedAt,
        accessTkn: this.accessToken, refreshTkn: this.refreshToken);
  }

  Map<String, dynamic> toJson() {
    _$AccountToJson(this);
  }
}
EN

Stack Overflow用户

回答已采纳

发布于 2019-11-18 14:50:45

因为没有初始化传递的参数,所以您得到了错误,因此您有一个空的构造函数。

您必须初始化类中的每个参数,或者允许它们在JsonSerializable(nullable: true)JsonKey(nullable: true)中为空。

如果这个解决方案不适合您,请共享您类中的所有代码。

编辑:

图书馆的工作与反思,我明白哪里是一个错误。

应该将parameters.

  • Your

  • 属性命名为与

  • getter相同的名称,与参数

命名相同。

用下面的修补程序更改代码:

代码语言:javascript
运行
复制
Account(String id, String firstName, String lastName, String email,
    DateTime dob, DateTime created, DateTime updated,
    String accessToken, String refreshToken) {
    this._id = id;
    this._firstName = firstName;
    this._lastName = lastName;
    this._email = email;
    this._dob = dob;
    this._created = created;
    this._updated = updated;
    this._accessToken = accessToken;
    this._refreshToken = refreshToken;
  }

  /// Get the date the account was created.
  DateTime get created {
    return this._created;
  }

  /// get teh date the account was last updated.
  DateTime get updated {
    return this._updated;
  }
票数 4
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58914535

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档