首页 > 解决方案 > 如何解决 [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");
        }
    });
}

有谁知道如何解决这个错误?

标签: jsonflutterdart

解决方案


该字符串User does not exist不是有效的 JSON。这就是错误消息告诉您的内容。

要成为有效的 JSON,它必须包含引号。

有两种选择:

  1. 更改服务器以将响应编码为 JSON。

  2. 不要尝试将响应解码为 JSON。

    只需使用:

    if(response.body == "User does not exist")
    

推荐阅读