特定错误的Dart错误处理
我想处理Dart中可能发生的不同错误。 我正在使用try/catch,但不知道如何确定可能发生的不同错误。 例如,当没有网络连接时,我出现以下错误:特定错误的Dart错误处理,dart,firebase-authentication,flutter,Dart,Firebase Authentication,Flutter,我想处理Dart中可能发生的不同错误。 我正在使用try/catch,但不知道如何确定可能发生的不同错误。 例如,当没有网络连接时,我出现以下错误: PlatformException(Error 17020, FIRAuthErrorDomain, Network error (such as timeout, interrupted connection or unreachable host) has occurred.) 当用户名/密码不正确时出现此错误: PlatformExcept
PlatformException(Error 17020, FIRAuthErrorDomain, Network error (such as timeout, interrupted connection or unreachable host) has occurred.)
当用户名/密码不正确时出现此错误:
PlatformException(Error 17009, FIRAuthErrorDomain, The password is invalid or the user does not have a password.)
我想根据发生的错误采取不同的措施。
这里最好的方法是什么?
更新:最终使用以下方法
import 'package:flutter/services.dart' show PlatformException;
try {
//Something!
} on PlatformException catch (e) {
switch (e.code) {
case "Error 17009":
// handle
break;
case "Error 17020":
// handle
break;
case "Error 17011":
//handle
break;
default:
throw new UnimplementedError(e.code);
}
}
我会使用
try
/catch
和开关
/案例
:
import 'package:flutter/services.dart' show PlatformException;
try {
...
} on PlatformException catch(e) {
switch(e.code) {
case '17009':
// handle
break;
case '17020':
// handle
break;
default:
throw new UnimplementedError(error.code);
}
}
找出
e.code
的实际值应该不会太难,这样您就可以修复案例:…
statements@LukeStanyer你介意把这个链接作为评论而不是我答案的内容来添加吗?这样可以清楚地知道是谁贡献了什么?这仍然有效吗?我尝试过,但我的错误显示为这样,完全没有代码PlatformException(异常,电子邮件地址已被另一个帐户使用,null)
Hi。你说得对。这已经不起作用了!我无法捕捉到异常!现在似乎有了.code、.message和.details,您可以根据异常读取和处理它们。我认为.code
已经不存在了。