flutter - 未处理的异常:“字符串”类型不是“地图”类型的子类型' 当在颤动中重置密码时
问题描述
我尝试使用颤振从 API 重置密码,但我得到了结果
Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 1 of 783 libraries in 1,240ms.
I/flutter ( 8568): {success: 1, message: Successfully Reset Password!}
E/flutter ( 8568): [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
E/flutter ( 8568): #0 UserResult.sqlreset (package:fluttermysql/models/sql_model.dart:66:42)
E/flutter ( 8568): <asynchronous suspension>
E/flutter ( 8568): #1 _ResetPasswordState.reset (package:fluttermysql/view/ResetPassword.dart:56:5)
E/flutter ( 8568): <asynchronous suspension>
E/flutter ( 8568):
这是我的模型
class UserResult {
bool status;
int success;
String message;
String emailAPI;
String password;
String phone_number;
UserResult(
{this.status, this.emailAPI, this.password, this.message, this.phone_number, this.success});
factory UserResult.createPostResult(Map<String, dynamic> data) {
return UserResult(
status : data['status'],
success : data['success'],
message : data['message'],
emailAPI: data["email"],
password: data["password"],
phone_number: data["phone_number"],
);
}
这是我的重置密码功能
static Future<UserResult> sqlreset(
{String email, String password, String idUser, String url}) async {
var response;
if (email != null && password != null) {
response = await http.post(url, body: jsonEncode(<String, String>{
'email': email,
'password': password,
}));
if (response.statusCode == 200) {
print(json.decode(response.body));
} else {
print(response.statusCode);
};
return UserResult.createPostResult(jsonDecode(json.encode(response.body.toString())));
}
response = await http.post(url, body: {
'email': "${email}",
'password': "${password}",
});
return UserResult.createPostResult(jsonDecode(json.encode(response.body.toString())));
}
json输出有问题吗?我还是不明白控制台日志中的错误是什么谢谢
解决方案
仅使用
return UserResult.createPostResult(jsonDecode(response.body));
因为你不需要一次又一次地编码和解码
推荐阅读
- java - 从firebase中删除孩子后应用程序崩溃
- javascript - \r\n 在 API 结果中 - 如何摆脱它
- r - 绘制链接到辅助 y 轴的折线图
- c# - 如何在 Xamarin 项目中重用 asp.net core dll?
- google-calendar-api - 412 通过带有 if-match 的 API 将与会者添加到重复的谷歌日历事件
- react-native - Undefined 不是函数 - 评估 require.getModules() || 反应原生 || 内联要求 || 启用解绑
- sql - sql:选择两列值的行
- c# - 从另一个类更改文本框
- python - 增强拓扑(NEAT)神经网络的神经进化可以在 TensorFlow 中构建吗?
- html - 菜单栏中的菜单宽度不合适