guccisekspir
guccisekspir

Reputation: 1377

Flutter bloc adding 2 event in same time

I wanna check users internet connection and firebase auth state changes in my app. I am using flutter bloc for my app's state management. But when call different 2 .add(event) in one initstate always the first one is run and changes states but second one didnt run didnt change state. What is the my wrong ?

my bloc:

class ControllerBloc extends Bloc<ControllerEvent, ControllerState> {
  ControllerBloc() : super(ControllerInitial());
  AuthApiClient _authApiClient = getIt<AuthApiClient>();

  @override
  Stream<ControllerState> mapEventToState(
    ControllerEvent event,
  ) async* {
    if (event is ControllInternetConnection) {
      yield* internetControll();
    }
    if (event is ControllUserAuth) {
      debugPrint("wwwwgeldi");
      yield* userAuthControl();
    }
    // TODO: implement mapEventToState
  }

  Stream<ControllerState> internetControll() async* {
    Stream<DataConnectionStatus> connectionState =
        DataConnectionChecker().onStatusChange;
    await for (DataConnectionStatus status in connectionState) {
      switch (status) {
        case DataConnectionStatus.connected:
          debugPrint("Bağlandı");
          yield InternetConnectedState();
          break;
        case DataConnectionStatus.disconnected:
          debugPrint("Kesildi");
          yield InternetConnectionLostState();
          break;
      }
    }
  }

  Stream<ControllerState> userAuthControl() async* {
    FirebaseAuth firebaseAuth = _authApiClient.authInstanceAl();
    debugPrint("geldi");
    Stream<User> authStream = firebaseAuth.authStateChanges();

    _authApiClient.authInstanceAl().signOut();

    await for (User authUserResult in authStream) {
      if (authUserResult == null) {
        yield UserAuthControlError();
      }
    }
  }
}

my page where call my events

class _NavigationPageState extends State<NavigationPage> {
  ControllerBloc controllerBloc;

   

  @override
  void initState() {
    controllerBloc= BlocProvider.of<ControllerBloc>(context);
    controllerBloc.add(ControllInternetConnection());
    controllerBloc.add(ControllUserAuth());
    super.initState();
  }

Upvotes: 2

Views: 2989

Answers (1)

theo
theo

Reputation: 159

If I am understanding this right, it looks to me that you are trying to solve two different problems with one BLoC. I don't see a reason why internet connection and user auth have to be in one BLoC, rather I would just separate the two in separate BLoCs.

As the discussion in this thread points out, the point of using a BLoC revolves around the idea of predictability purposes. You can override the existing BLoC event stream, but I personally think that is too complicated for what you are trying to do.

So I would suggest, either make two separate BLoCs or combine the entire process into one event, where the internet connection would be checked before the user is authenticated, you will then return different states depending on the errors.

Upvotes: 2

Related Questions