首页 > 解决方案 > 如何等待 firebase verifyPhoneNumber 回调?

问题描述

我正在尝试使用干净的架构转换我的应用程序。我正在关注 Reso Coder 的教程。我的问题是如何在数据源脚本中等待 firebaseverifyPhoneNumber回调?它将在回调触发之前返回到 repositoryImplimentation 脚本。我也是新来的集团。

ServerException这是一个示例脚本,当用户输入无效的电话号码格式时必须返回一个。

mobile_auth_datasource.dart

abstract class MobileAuthenticationDatasource {
  Future<void> verifyPhoneNumber(Params params);
}

class MobileAuthenticationDatasourceImpl
    extends MobileAuthenticationDatasource {
  String _verificationCode = "";

  MobileAuthenticationDatasourceImpl({this.firebaseAuth});

  @override
  Future<void> verifyPhoneNumber(Params params) async {
    await FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: params.phoneNumber,
        timeout: Duration(seconds: 0),
        verificationCompleted: (authCredential) =>
            _verificationComplete(authCredential),
        // if there is an exception, get the exception message and set it to the return value
        verificationFailed: (authException) =>
            _verificationFailed(authException),
        codeAutoRetrievalTimeout: (verificationId) =>
            _codeAutoRetrievalTimeout(verificationId),
        // called when the SMS code is sent
        codeSent: (verificationId, [code]) =>
            _smsCodeSent(verificationId, [code]));
  }   

  String _verificationFailed(AuthException authException) {
    print(authException.message);
    throw ServerException(message: 'Unexpected Error Occur');
    //return authException.message;
  }
}

mobile_auth_repository_impl.dart

class MobileAuthenticationRepositoryImpl
    implements MobileAuthenticationRepository {
  final MobileAuthenticationDatasource localDataSource;
  final NetworkInfo networkInfo;

  MobileAuthenticationRepositoryImpl({this.localDataSource, this.networkInfo});
  @override
  Future<Either<Failure, MobileAuthenticationEntity>> verifyPhoneNumber(
      Params params) async {
    try {
      await localDataSource.verifyPhoneNumber(params);
      return Right(null);
    } on ServerException catch (e) {
      return Left(ServerFailure(message: e.message));

    } on SocketException {
      return Left(NetworkFailure());
    }
  }
}

标签: flutterfirebase-authenticationblocclean-architecture

解决方案


推荐阅读