我未处理此错误异常:type'(动态)=
模型类
class UserLogin {
bool success;
int statusCode;
String code;
String message;
List<Data> data;
UserLogin({
this.code,
this.success,
this.message,
this.statusCode,
this.data,
});
UserLogin.fromJson(Map<String, dynamic>json){
success = json['success'];
statusCode = json['statusCode'];
code = json['code'];
message = json['message'];
//data = json['data'] != null ? new Data.fromJson(json['data']) : null;
if(json['data'] != null) {
data = new List<Data>();
json['data'].forEach((v){
data.add(new Data.fromJson(v));
});
}
}
Map<String, dynamic> toJson(){
final Map<String, dynamic> data = new Map<String, dynamic>();
data['success'] = this.success;
data['statuCode'] = this.statusCode;
data['code'] = this.code;
data['message'] = this.message;
if(this.data != null) {
data['data'] = this.data.toList();
}
return data;
}
@override toString() => this.toJson().toString();
}
class Data {
String token;
int id;
String email;
String nicename;
String firstname;
String lastname;
String displayName;
Data(
{this.token,
this.id,
this.email,
this.nicename,
this.firstname,
this.lastname,
this.displayName});
Data.fromJson(Map<String, dynamic> json){
token = json['token'];
id = json['id'];
email = json['email'];
nicename = json['nicename'];
firstname = json['firstname'];
displayName = json['displayName'];
}
Map<String, dynamic> toJson(){
final Map<String, dynamic> data = new Map<String, dynamic>();
data['token'] = this.token;
data['id'] = this.id;
data['email'] = this.email;
data['nicename'] = this.nicename;
data['firstname'] = this.firstname;
data['displayName'] = this.displayName;
return data;
}
}
调用api的异步函数
Future<UserLogin> loginCustomer(String username, String password ) async {
//List<UserLogin> model = new List<UserLogin>();
UserLogin model;
try {
var response = await Dio().post(
Config.tokenURL,
data: {
"username" : username,
"password" : password,
},
options: new Options(
headers: {
HttpHeaders.contentTypeHeader: "application/x-www-form-urlencoded",
},
),
);
if(response.statusCode == 200) {
print(response.data);
model = UserLogin.fromJson(response.data);
}
} on DioError catch (e) {
print(e.message);
}
return model;
}
Api的响应
{success: true, statusCode: 200, code: jwt_auth_valid_credential, message: Credential is valid, data: {token:***, id: 1, email: ***, nicename: ***, firstName: ***, lastName: ***, displayName: ****}}
更新代码
class UserLogin {
bool success;
int statusCode;
String code;
String message;
Data data;
UserLogin({
this.code,
this.success,
this.message,
this.statusCode,
this.data,
});
UserLogin.fromJson(Map<String, dynamic>json){
success = json['success'];
statusCode = json['statusCode'];
code = json['code'];
message = json['message'];
data = json['data'] != null ? new Data.fromJson(json['data']) : null;
}
Map<String, dynamic> toJson(){
final Map<String, dynamic> data = new Map<String, dynamic>();
data['success'] = this.success;
data['statuCode'] = this.statusCode;
data['code'] = this.code;
data['message'] = this.message;
if(this.data != null) {
data['data'] = this.data.toJson();
}
return data;
}
@override toString() => this.toJson().toString();
}
class Data {
String token;
int id;
String email;
String nicename;
String firstname;
String lastname;
String displayName;
Data(
{this.token,
this.id,
this.email,
this.nicename,
this.firstname,
this.lastname,
this.displayName});
Data.fromJson(Map<String, dynamic> json){
token = json['token'];
id = json['id'];
email = json['email'];
nicename = json['nicename'];
firstname = json['firstname'];
displayName = json['displayName'];
}
Map<String, dynamic> toJson(){
final Map<String, dynamic> data = new Map<String, dynamic>();
data['token'] = this.token;
data['id'] = this.id;
data['email'] = this.email;
data['nicename'] = this.nicename;
data['firstname'] = this.firstname;
data['displayName'] = this.displayName;
return data;
}
}
基于response.data
,您的数据是一个对象
,但您在UserLogin
模型类中将其定义为List
,这是错误的。
正确的应该是Data
;
这是显示类型列表动态不是类型映射字符串动态的子类型“现在当我运行模拟器时,显示”类型列表动态不是类型映射字符串动态的子类型“在我的模拟器上,我如何修复它? 现在我想创建关于查找api id的程序,从我的模拟器中使用文本字段和按钮,当我把一些数字和单击按钮时,会显示该ID的数据或标题,但我只是初学者,所以如果有人认为我的代码是正确的或不正确的或者你有推荐请告诉我 My JSON JSON代码 应用程
我取一些产品从服务器,但我得到了一个错误,说明另一个例外被抛出:不正确使用家长DataWidget。同步快照 这是我的ProductModel课程 这是主页 这是JsonResorts
我收到了错误消息: 被精确定位的线在这里: 返回类型为
我正在尝试从服务器获取json数据
我正在开发我的flutter应用程序的身份验证。请帮我解决这个问题。我无法识别返回这样一个错误的问题,声明未处理异常:类型列表不是类型字符串的子类型。 以下是登录屏幕的代码: 执行登录的上述代码部分: 错误