【问题标题】:Flutter Format Exception : Unexpected Character (at character 1)颤振格式异常:意外字符(在字符 1 处)
【发布时间】:2020-08-12 13:37:08
【问题描述】:

我在 Flutter 项目中遇到异常。错误如下所示:

Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)

这显示在“抛出错误”行中。代码如下:

Future<void> _authenticate(
      String email, String password, String urlSegment) async {
    final url =
        "https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=AIzaSyC9Rz9CDv-_hc68I_wdvogF2ZnDHCpr2Y8";
    try {
      final response = await http.post(
        url,
        body: json.encode(
          {
            "email": email,
            "password": password,
            "returnSecureToken": true,
          },
        ),
      );
      print(response.body);
      final responseData = json.decode(response.body);
      if (responseData["error"] != null) {
        throw HttpException(responseData["error"]["message"]);
      }
      _token = responseData["idToken"];
      _userId = responseData["localId"];
      _expiryDate = DateTime.now().add(
        Duration(
          seconds: int.parse(
            responseData["expiresIn"],
          ),
        ),
      );
      _autoLogout();
      notifyListeners();
      final prefs = await SharedPreferences.getInstance();
      final userData = json.encode(
        {
          "token": _token,
          "userId": _userId,
          "expiryDate": _expiryDate.toIso8601String(),
        },
      );
      prefs.setString("userData", userData);
    } catch (error) {
      throw error;           //Exception is showing here
    }
  }

Error message looks like this as shown in the image (Click)

调试控制台显示以下消息:

I/flutter (18432): <!DOCTYPE html>
I/flutter (18432): <html lang=en>
I/flutter (18432):   <meta charset=utf-8>
I/flutter (18432):   <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
I/flutter (18432):   <title>Error 404 (Not Found)!!1</title>
I/flutter (18432):   <style>
I/flutter (18432):     *{margin:0;padding:0}html,code{font:15px/22px arial,sans-serif}html{background:#fff;color:#222;padding:15px}body{margin:7% auto 0;max-width:390px;min-height:180px;padding:30px 0 15px}* > body{background:url(//www.google.com/images/errors/robot.png) 100% 5px no-repeat;padding-right:205px}p{margin:11px 0 22px;overflow:hidden}ins{color:#777;text-decoration:none}a img{border:0}@media screen and (max-width:772px){body{background:none;margin-top:0;max-width:none;padding-right:0}}#logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo_color_150x54dp.png) no-repeat;margin-left:-5px}@media only screen and (min-resolution:192dpi){#logo{background:url(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png) no-repeat 0% 0%/100% 100%;-moz-border-image:url(//www.google.com/images/brand
I/flutter (18432): FormatException: Unexpected character (at character 1)
I/flutter (18432): <!DOCTYPE html>
I/flutter (18432): ^

请帮忙

【问题讨论】:

  • 您的 HTTP 请求导致 404 Not Found 错误,因此您的 HTTP 响应包含通常会显示的错误页面的内容。
  • @jamesdlin 感谢回复,但请告诉我如何解决。
  • 好吧,您需要修复您的 HTTP 请求,使其到达正确的位置。目前它会产生 404 错误。您需要自己调试。验证请求的 URL 是否符合您的预期。

标签: firebase flutter dart firebase-authentication flutter-layout


【解决方案1】:

您似乎没有收到 JSON 作为响应,因此当您解码时,它会抛出该异常。在 postman 中试用 API 看看响应是否为 JSON。

【讨论】:

  • 邮递员和我用的不一样吗?我对 firebase api 了解不多,所以如果你能更清楚地告诉我我能做些什么来修复我的代码,我将不胜感激。
【解决方案2】:

我的端点在 http url 中是错误的。我在我的 http url 中使用了一些较旧的端点。

根据 Firebase REST Auth 文档,我必须将signUp 用于注册功能,将signInWithPassword 用于登录功能,并将其传递给_authenticate() 中的urlSegment

【讨论】:

    【解决方案3】:

    我在 API 响应中渲染 pdf 文本和 html 文本时遇到了同样的问题,我已经通过以下代码解决了这个问题。更多请参考here

    var data= await json.decode(json.encode(response.databody);   
    

    注意:您必须输入json.encode 才能重新关注该问题

    【讨论】:

      【解决方案4】:

      如果您使用 Dio 并遇到这种错误,请添加:

      responseType: ResponseType.plain,
      

      给您的 dio 客户。完整的dio客户端如下:

      final Dio _dio = Dio(BaseOptions(
       connectTimeout: 10000,
       receiveTimeout: 10000,
      baseUrl: ApiEndPoints.baseURL,
      contentType: 'application/json',
       responseType: ResponseType.plain,
       ))
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-07-31
        • 2020-02-04
        • 1970-01-01
        • 2019-09-17
        • 2020-03-07
        • 2022-11-07
        • 2022-09-29
        • 1970-01-01
        相关资源
        最近更新 更多