'Can i wrap a Stream in try-catch block in Dart

This is a function for Phone number auth using firebase, _signInWithPhone is a Stream.

void signInWithPhone() {

    emit(state.copyWith(status: FormzStatus.submissionInProgress));
    try {
      _phoneNumberSignInSubscription = _signInWithPhone(
              phoneNumber: state.phone.value, timeout: verificationCodeTimeout)
          .listen((event) {
        emit(
          state.copyWith(verificationId: event),
        );
      });
    } on PhoneAuthError catch (e) {
      emit(state.copyWith(
          status: FormzStatus.submissionFailure, errorMessage: e.toString()));
    } catch (_) {
      emit(state.copyWith(status: FormzStatus.submissionFailure));
    }

How best I can handle the errors without try/catch block. I believe the try/catch block would result in not catching the error as expected. Pls suggest a better approach and it should work efficiently with Flutter Bloc/Cubit. Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source