Firebase 颤振格式异常:意外字符(字符1处)

Firebase 颤振格式异常:意外字符(字符1处),firebase,flutter,dart,firebase-authentication,flutter-layout,Firebase,Flutter,Dart,Firebase Authentication,Flutter Layout,我在颤振项目中遇到了一个例外。错误如下所示: Exception has occurred. FormatException (FormatException: Unexpected character (at character 1) <!DOCTYPE html> ^ ) 发生异常。 FormatException(FormatException:意外字符(在字符1处) ^ ) 这显示在“抛出错误”行中。代码如下: Future<void> _authentica

我在颤振项目中遇到了一个例外。错误如下所示:

Exception has occurred.
FormatException (FormatException: Unexpected character (at character 1)
<!DOCTYPE html>
^
)
发生异常。 FormatException(FormatException:意外字符(在字符1处) ^ ) 这显示在“抛出错误”行中。代码如下:

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
    }
  }
Future\u认证(
字符串电子邮件、字符串密码、字符串URL(段)异步{
最终url=
"https://identitytoolkit.googleapis.com/v1/accounts:$URLSEMENT?key=AIzaSyC9Rz9CDv-_hc68I_WDVOGF2ZNDHCR2Y8”;
试一试{
最终响应=等待http.post(
网址,
正文:json.encode(
{
“电子邮件”:电子邮件,
“密码”:密码,
“returnSecureToken”:正确,
},
),
);
打印(响应.正文);
final responseData=json.decode(response.body);
if(responseData[“error”]!=null){
抛出HttpException(responseData[“error”][“message”]);
}
_令牌=响应数据[“idToken”];
_userId=responseData[“localId”];
_expireydate=DateTime.now().add(
持续时间(
秒:int.parse(
响应数据[“expiresIn”],
),
),
);
_自体();
notifyListeners();
final prefs=wait SharedPreferences.getInstance();
final userData=json.encode(
{
“令牌”:,
“userId”:\u userId,
“expiryDate”:_expiryDate.toiso8601字符串(),
},
);
prefs.setString(“userData”,userData);
}捕获(错误){
抛出错误;//此处显示异常
}
}

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

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): ^
I/颤振(18432):
I/颤振(18432):
I/颤振(18432):
I/颤振(18432):
I/颤振(18432):错误404(未找到)!!1.
I/颤振(18432):
I/flatter(18432):*{margin:0;padding:0}html,代码{font:15px/22px arial,sans serif}html{background:#fff;color:#222;padding:15px}body{margin:7%自动0;最大宽度:390px;最小高度:180px;padding:30px 0 15px}>body{background背景:url(//www.google.com/images/errors/robot.png)100%5px不重复;padding右侧:205px}ins{color:#777;文本装饰:none}一个img{border:0}@media屏幕和(最大宽度:772px){body{background:none;margin top:0;max width:none;padding right:0}}logo{background:url(//www.google.com/images/branding/googlelogo/1x/googlelogo/1x/googlelogo_color_150x54dp.png)不重复;margin left:-5px}@media-only屏幕和(最小分辨率:192dpi){background:url}(//www.google.com/images/branding/googlelogo/2x/googlelogo_color_150x54dp.png)不重复0%0%/100%100%;-moz边框图像:url(//www.google.com/images/brand)
I/flatter(18432):FormatException:意外字符(字符1处)
I/颤振(18432):
I/颤振(18432):^

请帮助

您似乎没有得到JSON作为响应,因此当您解码时,它会引发该异常。请在postman中尝试API以查看响应是否为JSON。

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


根据Firebase REST Auth文档,我必须对注册函数使用
signUp
,对登录函数使用
signInWithPassword
,并将其传递到
\u authenticate()中的
urlsgment

我在API响应中呈现pdf文本和html文本时遇到了相同的问题,我已使用以下代码解决了此问题。有关更多信息,请参阅


注意:您必须放入
json.encode
以重新删除该问题

您的HTTP请求导致404未找到错误,因此您的HTTP响应包含通常会显示的错误页面内容。@jamesdlin Thanx用于回复,但请告诉我如何修复它。您需要修复HTTP请求,以便转到正确的位置。当前它生成404错误。您需要自己调试。验证请求的URL是否符合您的预期。postman与我使用的不同吗?我对firebase api不太了解,因此如果您能更清楚地告诉我如何修复代码,我将不胜感激。它正在工作,th脚踝男
var data= await json.decode(json.encode(response.databody);