我正在使用json_serializable、json_annotation和build为我的模型生成serialization/deserialization功能。但是,当我运行构建时,我会得到这个错误。
运行
错误JsonSerializableGenerator无法填充所需的构造函数参数:已创建。package:explorer/models/Account/account.dart:46:3
它所指的行是我的模型构造函数,即。
Account(String id, String firstName, String lastName, String email,
DateTime dob, DateTime created, DateTime updated,
{String accessTkn, String refreshTkn}) {}我为什么要犯这个错误?
按照要求,这是我的模型类。
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);
}
}发布于 2019-11-18 14:50:45
因为没有初始化传递的参数,所以您得到了错误,因此您有一个空的构造函数。
您必须初始化类中的每个参数,或者允许它们在JsonSerializable(nullable: true)或JsonKey(nullable: true)中为空。
如果这个解决方案不适合您,请共享您类中的所有代码。
编辑:
图书馆的工作与反思,我明白哪里是一个错误。
应该将parameters.
命名相同。
用下面的修补程序更改代码:
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;
}发布于 2020-10-15 13:23:11
为供日后参考,我想以一个例子解释上述问题,并提出一般的解决办法:
json_serializable + json_annotation使用构造函数参数名称作为json字段键。因此,以下两个例子之间有一个明显的区别:
@JsonSerializable()
class User {
@JsonKey(name: "first_name") final String firstName;
// In this case, the json key becomes 'first_name',
// extracted from the explicitly referenced field annotation.
const User(this.firstName);
}
@JsonSerializable()
class User {
@JsonKey(name: "first_name") String _firstName;
String get firstName => _firstName?.trim();
// In this case, the json key becomes 'firstName',
// extracted from the constructor parameter name.
// For reflection, the field and its annotation are not involved.
User(String firstName) {
this._firstName = firstName;
}
}我们想隐藏字段的原因有两个;我们不希望其他人能够更新它的值,我们希望提供一个“校正的”(在本例中是修剪的)值,而不是从外部源检索的未经验证的值。由于我们无法巧妙地隐藏未经验证的值,我建议我们公开它,但明确指出它的缺点,如下所示:
@JsonSerializable()
class User {
// The field is final, so its value cannot be altered as if private.
// It is exposed (sadly), but clearly mentions potential issues.
@JsonKey(name: "first_name") final String firstNameUntrimmed;
// This is the 'corrected' version available with a more pleasant field name.
String get firstName => firstNameUntrimmed?.trim();
const User(this.firstNameUntrimmed);
}https://stackoverflow.com/questions/58914535
复制相似问题