Authentication 颤振http异常

Authentication 颤振http异常,authentication,flutter,exception,dart,Authentication,Flutter,Exception,Dart,所以,在学习一些教程的同时,我遇到了一个奇怪的异常,它不断出现并冻结了我的应用程序 当我抛出异常时会出现此异常: 这是我的异常处理屏幕 class Auth with ChangeNotifier { String _token; DateTime _expiryDate; String _userId; Future<void> _authenticate( String email, String password, String urlSegme

所以,在学习一些教程的同时,我遇到了一个奇怪的异常,它不断出现并冻结了我的应用程序

当我抛出异常时会出现此异常:

这是我的异常处理屏幕

class Auth with ChangeNotifier {
  String _token;
  DateTime _expiryDate;
  String _userId;

  Future<void> _authenticate(
      String email, String password, String urlSegment) async {
    final url =
        'https://identitytoolkit.googleapis.com/v1/accounts:$urlSegment?key=YOUR_API_KEY';
    try {
      final response = await http.post(
        url,
        body: json.encode(
          {
            'email': email,
            'password': password,
            'returnSecureToken': true,
          },
        ),
      );
      final responseData = json.decode(response.body);
      print(responseData);
      if (responseData['error'] != null) {
        print("FUCK");
        throw HttpException(responseData['error']['message']);
      }
    } on HttpException catch (error) {
      print(error);
      throw error;
    }
  }

  Future<void> signUp(String email, String password) async {
    return _authenticate(email, password, 'signUp');
  }

  Future<void> logIn(String email, String password) async {
    return _authenticate(email, password, 'signInWithPassword');
  }
}
使用ChangeNotifier进行类身份验证{
字符串标记;
日期时间_expiryDate;
字符串_userId;
未来(
字符串电子邮件、字符串密码、字符串URL(段)异步{
最终url=
'https://identitytoolkit.googleapis.com/v1/accounts:$URLSEMENT?key=您的_API_key';
试一试{
最终响应=等待http.post(
网址,
正文:json.encode(
{
“电子邮件”:电子邮件,
“密码”:密码,
“returnSecureToken”:true,
},
),
);
final responseData=json.decode(response.body);
打印(应答数据);
if(responseData['error']!=null){
打印(“操”);
抛出HttpException(responseData['error']['message']);
}
}关于HttpException捕获(错误){
打印(错误);
投掷误差;
}
}
未来注册(字符串电子邮件、字符串密码)异步{
返回“身份验证”(电子邮件、密码、“注册”);
}
未来登录(字符串电子邮件、字符串密码)异步{
返回“验证(电子邮件、密码、“使用密码登录”);
}
}
为什么抛出错误? 如果你不处理它,在你的应用程序的其他地方,它可能会冻结甚至崩溃应用程序。 您应该处理此异常,而不是在try-catch中重新抛出或执行此方法,并在catch-bloc中处理异常

正如我所说,您有两种选择:

  • 删除
    抛出错误
  • 将执行放到另一个
    尝试catch
    适当地处理它(不抛出)(通常只记录异常以供进一步调查)

  • 我可以理解为什么在调试程序启动时,它会在调试构建期间暂停应用程序,但你的意思是它会在发布模式下冻结应用程序吗?顺便说一句,你可以使用
    rethrow这是否回答了您的问题?我在另一个屏幕上处理它,但问题是当抛出错误时出现异常。异常不会被抛出到我处理它的屏幕上。我附上了错误处理的图片。你们能显示最后一个捕获块吗?似乎您的重试错误出现在这里,但我们需要完整的示例来理解原因