Bloc: Login with Firebase Auth Example

Created on 22 Sep 2019  路  7Comments  路  Source: felangel/bloc

Hi ,
did you try Firebase Auth with bloc ?
because it use callback for response, I don't know how to use it with Future.

await _firebaseAuth.verifyPhoneNumber(
        phoneNumber: _phoneNumber,
        codeSent: phoneCodeSent,
        verificationCompleted: phoneVerificationCompleted,
        verificationFailed: phoneVerificationFailed,
        timeout: Duration(seconds: 5),
        codeAutoRetrievalTimeout: autoRetrievalTimeout);

Thank

question

Most helpful comment

I worked with phone auth with bloc and firebase, the callbacks should be in the bloc and dispatch events

All 7 comments

I worked with phone auth with bloc and firebase, the callbacks should be in the bloc and dispatch events

thank you.

hi @bigworld12
I tried with StreamController but in case I make a dispatch again. It didnot work. OnEvent called but mapEventToState not call again.
onEvent RegisterBloc LoadRegisterEvent .

I think my using of stream is not correct. Can you give me some advices. thanks

class RegisterBloc extends Bloc<RegisterEvent, RegisterState> {

  StreamController<RegisterState> streamController = new StreamController();

  RegisterState get initialState {
    return new RegisterStateInit();
  }

  @override
  Stream<RegisterState> mapEventToState(
    RegisterEvent event,
  ) async* {

    try {
      if(event is LoadRegisterEvent){
        yield RegisterStateLoading();
        f(event.phoneNumber);
        yield* streamController.stream;
      }

    } catch (_, stackTrace) {
      print('$_ $stackTrace');
    }
  }
  @override
  void dispose() {
    print("disposed");
    super.dispose();
     streamController.close();
  }

  void f(String _phoneNumber) async{
     final PhoneCodeAutoRetrievalTimeout autoRetrievalTimeout = (String verId) {
      print("autoRetrievalTimeout");
      streamController.add(RegisterStateError('Timeout'));
    };
    PhoneCodeSent phoneCodeSent = (String verId, [int forceCodeResend]) {
      print("phoneCodeSent" + verId + " " + forceCodeResend.toString());
      var phoneCodeSentModel = phoneCodeSentResponse(verId, true);
      streamController.add(RegisterStateLoaded(phoneCodeSentModel.verificationCode));
    };

    PhoneVerificationCompleted phoneVerificationCompleted =
        (AuthCredential credential) async {
      print("Success");
      // var phoneCodeSentModel = phoneCodeSentResponse("verId", true);
      // streamController.add(RegisterStateLoaded(phoneCodeSentModel.verificationCode));
    };

    final PhoneVerificationFailed phoneVerificationFailed =
        (AuthException exception) {
      print("phoneVerificationFailed");

      streamController.add(RegisterStateError(exception.message));
    };
    FirebaseAuth.instance.verifyPhoneNumber(
        phoneNumber: _phoneNumber,
        codeSent: phoneCodeSent,
        verificationCompleted: phoneVerificationCompleted,
        verificationFailed: phoneVerificationFailed,
        timeout: Duration(seconds: 0),
        codeAutoRetrievalTimeout: autoRetrievalTimeout);
  }
  PhoneCodeSentModel phoneCodeSentResponse(String verificationId, bool isSuccess){
      return PhoneCodeSentModel(verificationId, isSuccess);
  }
}

why did you create a stream controller when you can use
dispatch(RegisterStateLoaded(phoneCodeSentModel.verificationCode));

because I need to create another event for it. cannot dispatch with state.

btw, we cannot use Stream inside mapEventToState?

oh I didn't notice that was a stream of states,
well yes, you need to create events.
Bloc is already a stream of states, that's what mapEventToState is for, it responds to events fired from dispatch, and yields states as an output

@nguyenhuutinh have you taken a look at the Firebase Login Tutorial?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

clicksocial picture clicksocial  路  3Comments

nhwilly picture nhwilly  路  3Comments

hivesey picture hivesey  路  3Comments

komapeb picture komapeb  路  3Comments

abinvp picture abinvp  路  3Comments