Flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Instance of 'CustomException'
Flutter: [ERROR:flutter/lib/ui/ui_dart_state.cc(199)] Unhandled Exception: Instance of 'CustomException'
我正在尝试使用 firebase auth 执行移动身份验证。在执行注册之前的注册过程中,我正在检查手机号码是否已经存在,如果存在,我将抛出自定义异常。
代码
class CustomException implements Exception {
String cause;
CustomException(this.cause);
}
Future mobileAuth(
String number, BuildContext context, Customer newCustomer) async {
try {
_databaseService
.customerStream(customer: newCustomer)
.listen((event) async {
//Throwing custom exception if user exists
if (event.length > 0) {
throw CustomException("got error user");
}
await _firebaseAuth.verifyPhoneNumber(
phoneNumber: number,
verificationCompleted: (PhoneAuthCredential credential) async {
await FirebaseAuth.instance
.signInWithCredential(credential)
.then((value) async {
if (value.user != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => HomeScreen(index: 3)));
}
});
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
print('The provided phone number is not valid.');
}
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MobileError()));
},
codeSent: (String verificationId, int? resendToken) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SignupForm3(
phoneNumber: number,
verificationCode: verificationId,
newCustomer: newCustomer,
)));
},
codeAutoRetrievalTimeout: (String verificationId) {
},
);
});
return true;
} on CustomException catch (e) {
print("User already exists ");
print(e.cause);
return false;
} on FirebaseAuthException catch (e) {
return false;
} on PlatformException catch (e) {
print(e.message);
return false;
} catch (e) {
print(e.toString());
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MobileError2()));
return false;
}
}
我收到这个未处理的异常错误。但是我已经实现了 catch 来处理这个自定义异常。
[错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:'CustomException'
的实例
我认为您的 if check 传递错误,因此永远不会调用您的异常。
很可能事件的长度总是大于 0 所以你的 if check总会过去的。
但是你可以查看文档,看看他们是否有一个实现来检查类似的东西。
侦听器未捕获异常。
所以需要在监听回调中添加一个try-catch
:
_databaseService
.customerStream(customer: newCustomer)
.listen((event) async {
try {
//Listener code
} on CustomException catch (e) {
//Handle CustomException
} catch (e) {
//Handle other Exceptions
}
}
我正在尝试使用 firebase auth 执行移动身份验证。在执行注册之前的注册过程中,我正在检查手机号码是否已经存在,如果存在,我将抛出自定义异常。
代码
class CustomException implements Exception {
String cause;
CustomException(this.cause);
}
Future mobileAuth(
String number, BuildContext context, Customer newCustomer) async {
try {
_databaseService
.customerStream(customer: newCustomer)
.listen((event) async {
//Throwing custom exception if user exists
if (event.length > 0) {
throw CustomException("got error user");
}
await _firebaseAuth.verifyPhoneNumber(
phoneNumber: number,
verificationCompleted: (PhoneAuthCredential credential) async {
await FirebaseAuth.instance
.signInWithCredential(credential)
.then((value) async {
if (value.user != null) {
Navigator.of(context).pushReplacement(MaterialPageRoute(
builder: (context) => HomeScreen(index: 3)));
}
});
},
verificationFailed: (FirebaseAuthException e) {
if (e.code == 'invalid-phone-number') {
print('The provided phone number is not valid.');
}
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MobileError()));
},
codeSent: (String verificationId, int? resendToken) {
Navigator.of(context).push(MaterialPageRoute(
builder: (context) => SignupForm3(
phoneNumber: number,
verificationCode: verificationId,
newCustomer: newCustomer,
)));
},
codeAutoRetrievalTimeout: (String verificationId) {
},
);
});
return true;
} on CustomException catch (e) {
print("User already exists ");
print(e.cause);
return false;
} on FirebaseAuthException catch (e) {
return false;
} on PlatformException catch (e) {
print(e.message);
return false;
} catch (e) {
print(e.toString());
Navigator.of(context)
.push(MaterialPageRoute(builder: (context) => MobileError2()));
return false;
}
}
我收到这个未处理的异常错误。但是我已经实现了 catch 来处理这个自定义异常。
[错误:flutter/lib/ui/ui_dart_state.cc(199)] 未处理的异常:'CustomException'
的实例我认为您的 if check 传递错误,因此永远不会调用您的异常。
很可能事件的长度总是大于 0 所以你的 if check总会过去的。
但是你可以查看文档,看看他们是否有一个实现来检查类似的东西。
侦听器未捕获异常。
所以需要在监听回调中添加一个try-catch
:
_databaseService
.customerStream(customer: newCustomer)
.listen((event) async {
try {
//Listener code
} on CustomException catch (e) {
//Handle CustomException
} catch (e) {
//Handle other Exceptions
}
}