gpt4 book ai didi

json - 错误: 'List' is not a subtype of type 'Map'

转载 作者:行者123 更新时间:2023-12-03 08:24:34 27 4
gpt4 key购买 nike

我试图在用户输入电子邮件和密码后获取用户信息。我正在使用的 API 返回用户的额外信息,因此我尝试获取该信息作为开始。

我正在尝试解析这个 json:

[
{
"_user": {
"id": "id-here",
"name": "Mobile",
"email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="44292b262d28216a31372136042537206a272b29" rel="noreferrer noopener nofollow">[email protected]</a>",
"photo": null,
"title": null,
"surname": "User",
"bg_photo": null,
"isactive": true,
"password": "123456789",
"username": "mobileuser",
"checkInfo": true,
"role_type": "asd",
"profession": null,
"isemailverify": false
},
"tokens": {
"accessToken": "someTokenhere",
"refreshToken": "anotherOne"
}
}
]

这些是我的模型:

class User {
User({
this.user,
this.tokens,
});

UserClass user;
Tokens tokens;

factory User.fromJson(Map<String, dynamic> json){
return User(
user: UserClass.fromJson(json["_user"]),
tokens: Tokens.fromJson(json["tokens"]),
);
}
}

class Tokens {
Tokens({
this.accessToken,
this.refreshToken,
});

String accessToken;
String refreshToken;

factory Tokens.fromJson(Map<String, dynamic> json) => Tokens(
accessToken: json["accessToken"],
refreshToken: json["refreshToken"],
);
}

class UserClass {
UserClass({
this.id,
this.name,
this.email,
this.photo,
this.title,
this.surname,
this.bgPhoto,
this.isactive,
this.password,
this.username,
this.checkInfo,
this.roleType,
this.profession,
this.isemailverify,
});

String id;
String name;
String email;
String photo;
String title;
String surname;
String bgPhoto;
bool isactive;
String password;
String username;
bool checkInfo;
String roleType;
String profession;
bool isemailverify;

factory UserClass.fromJson(Map<String, dynamic> json) => UserClass(
id: json["id"],
name: json["name"],
email: json["email"],
photo: json["photo"],
title: json["title"],
surname: json["surname"],
bgPhoto: json["bg_photo"],
isactive: json["isactive"],
password: json["password"],
username: json["username"],
checkInfo: json["checkInfo"],
roleType: json["role_type"],
profession: json["profession"],
isemailverify: json["isemailverify"],
);
}

这是我发送电子邮件、密码和获取用户数据的部分:

Future<User> checkUserExist(String email, String password) async {
final response = await http.post(
Uri.https('SomeApi', 'Route'),
headers: <String, String>{
'Content-Type': 'application/json; charset=UTF-8',
},
body: jsonEncode(<String, String>{
'email': email,
'password': password
}),
);
if (response.statusCode == 200) {
return User.fromJson(jsonDecode(response.body));
} else {
throw Exception(jsonDecode(response.body));
}
}

这是给我错误的部分

if (response.statusCode == 200) {
return User.fromJson(jsonDecode(response.body));
}

我打印了响应正文,它给了我我想要的东西 - 字符串 - 但 jsonDecode 返回一个列表,所以我无法使用它。我怎样才能解决这个问题?提前致谢。

最佳答案

改变这个

return User.fromJson(jsonDecode(response.body))

对此:

return User.fromJson(jsonDecode(response.body)[0])

原因是你每件事都做得很完美,直到最后一步。如果仔细查看您的响应,它确实有一个映射或 _usertoken,但该映射实际上位于列表内,请注意方括号 [ ] 围绕 {}

您需要访问的 map 位于此列表的索引[0]处,那么一切都会正常工作。

关于json - 错误: 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66956501/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com