json - 如何解决 [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常:FormatException: Unexpected character (at character 1)
问题描述
我在 Flutter 中做一个登录功能。我想使用警报消息中显示的错误消息。现在我收到此错误:
E/flutter ( 2690): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: FormatException: Unexpected character (at character 1)
E/flutter ( 2690): User does not exist
E/flutter ( 2690): ^
这是我的代码:
Future login(String url, {Map body}) async {
return http.post(url, body: body).then((http.Response response) {
print(response.body);
if(json.decode(response.body) == "User does not exist") {
print("wrong email");
}
});
}
有谁知道如何解决这个错误?
解决方案
该字符串User does not exist
不是有效的 JSON。这就是错误消息告诉您的内容。
要成为有效的 JSON,它必须包含引号。
有两种选择:
更改服务器以将响应编码为 JSON。
不要尝试将响应解码为 JSON。
只需使用:
if(response.body == "User does not exist")
推荐阅读
- python - 为什么 numpy 的 as_strided 用(默认)strides=None 改变数组步幅?
- android - Corner radius property missing on MaterialButton after update to Material 1.2.0
- python - 使用多处理从队列中读取
- reactjs - 挂钩父母在孩子之前卸下
- python - 'latin-1' 编解码器无法对位置 51732-51735 中的字符进行编码:序数不在范围内(256)Django
- vue.js - 安装 vue-storefront 时出错
- javascript - 创建一个新的 Angular 项目
- string - bash脚本中奇怪/意外的字符串替换
- pandas - 多处理熊猫中的模糊匹配
- javascript - WooCommerce Nodejs Wrapper 返回未定义