Json Serializable - 无效参数(输入):不能为空
Posted
技术标签:
【中文标题】Json Serializable - 无效参数(输入):不能为空【英文标题】:Json Serializable - Invalid argument(s) (input): Must not be null 【发布时间】:2020-09-07 17:52:20 【问题描述】:我正在尝试在我的模型类中设置 JSON 值。但它的返回错误为 Invalid argument(s) (input): Must not be null
UserAccount.dart
import 'package:json_annotation/json_annotation.dart';
part 'userAccount.g.dart';
@JsonSerializable(nullable: true,disallowUnrecognizedKeys:true,includeIfNull:true)
class UserAccount
String id;
String name;
String email;
String profile_pic;
String country;
String phoneNumber;
bool isNewUser;
int role;
int account_status;
String token;
DateTime token_expiry;
String facebook_profile_url;
String google_profile_url;
String twitter_profile_url;
int matchStixx_count;
DateTime created_at;
DateTime updated_at;
UserAccount(
this.id,
this.name,
this.email,
this.profile_pic,
this.country,
this.phoneNumber,
this.isNewUser,
this.role,
this.account_status,
this.token,
this.token_expiry,
this.facebook_profile_url,
this.google_profile_url,
this.twitter_profile_url,
this.matchStixx_count,
this.created_at,
this.updated_at
);
factory UserAccount.fromJson(Map<String,dynamic> json) => _$UserAccountFromJson(json);
Map<String,dynamic> toJson() =>_$UserAccountToJson(this);
UserAccount.g.dart
part of 'userAccount.dart';
UserAccount _$UserAccountFromJson(Map<String, dynamic> json)
return UserAccount(
id: json['_id'] as String,
name: json['name'] as String,
email: json['email'] as String,
profile_pic: json['profile_pic'] as String,
country: json['country'] as String,
phoneNumber: json['phoneNumber'] as String,
isNewUser: json['isNewUser'] as bool,
role: json['role'] as int,
account_status: json['account_status'] as int,
token: json['token'] as String,
token_expiry: DateTime.parse(json['token_expiry'] as String),
facebook_profile_url: json['facebook_profile_url'] as String,
google_profile_url: json['google_profile_url'] as String,
twitter_profile_url: json['twitter_profile_url'] as String,
matchStixx_count: json['matchStixx_count'] as int,
created_at: DateTime.parse(json['created_at'] as String),
updated_at: DateTime.parse(json['updated_at'] as String)
);
Map<String, dynamic> _$UserAccountToJson(UserAccount instance) =>
<String, dynamic>
'id': instance.id,
'name': instance.name,
'email': instance.email,
'profile_pic': instance.profile_pic,
'country': instance.country,
'phoneNumber': instance.phoneNumber,
'isNewUser': instance.isNewUser,
'role': instance.role,
'account_status': instance.account_status,
'token': instance.token,
'token_expiry': instance.token_expiry,
'facebook_profile_url': instance.facebook_profile_url,
'google_profile_url': instance.google_profile_url,
'twitter_profile_url': instance.twitter_profile_url,
'matchStixx_count': instance.matchStixx_count,
'created_at': instance.created_at,
'updated_at': instance.updated_at,
;
JSON 数据 - 此 JSON 存储在 (responseBody["userData"])
_id: 1, name:some name, email: someName@domain.com, profile_pic: https://xyz/yzz.png, isNewUser: true, 角色: 1, account_status: 1, matchStixx_count: 20, created_at: 2020-05-15T06:41:24.528Z, __v: 0
模型初始化
userAccount = new UserAccount.fromJson(responseBody["userData"]); //geting error here
【问题讨论】:
【参考方案1】:如果您发布完整的堆栈跟踪会容易得多,因为它包含有关哪些字段为空的信息:
Unhandled exception:
Invalid argument(s) (input): Must not be null
#0 _RegExp.firstMatch (dart:core-patch/regexp_patch.dart:222:24)
#1 DateTime.parse (dart:core/date_time.dart:282:22)
#2 _$UserAccountFromJson (file:///***.dart:64:30)
#3 new UserAccount.fromJson (file:///***.dart:47:7)
#4 main (file:///***.dart:5:15)
#5 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
#6 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
代码中有问题的行如下:
token_expiry: DateTime.parse(json['token_expiry'] as String),
由于您的 json 不包含 token_expiry
,因此此处的结果将为空。但由于 DateTime.parse
不接受 null 作为输入,您会收到错误“不得为 null”。
对于修复,您可以将行更改为以下内容:
token_expiry: json['token_expiry'] == null ? null : DateTime.parse(json['token_expiry'] as String),
这将为字段updated_at
提供相同的错误,因此您也应该正确执行:
created_at: json['created_at'] == null ? null : DateTime.parse(json['created_at'] as String),
updated_at: json['updated_at'] == null ? null : DateTime.parse(json['updated_at'] as String));
【讨论】:
运行flutter pub run build_runner
时应该自动生成“UserAccount.g.dart”中的代码吧?也许@JsonKey(defaultValue: DateTime.Now()) DateTime <field>;
会为DateTime
字段创建后备值?【参考方案2】:
如果有人路过并遇到同样的问题,您需要验证您的变量名称,例如您是否有 Map<String, dynamic>
,那么您需要验证发生在我身上的键名,希望这有助于祝您好运
【讨论】:
以上是关于Json Serializable - 无效参数(输入):不能为空的主要内容,如果未能解决你的问题,请参考以下文章
在 nullsafety 之后使用带有 json_serializable 的 firestore
MySQL JSON 查询:参数 1 中的 JSON 文本无效
为啥我在 Laravel 中为 foreach 提供无效的参数以进行 json 响应?