1

I need to access AuthenticationBloc in my LoginBloc so I can fire the AuthenticationLogin() event if the login is successful. What I did so far is not working.

What I've done:

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  final AuthenticationBloc authenticationBloc;
  final AuthenticateCredentialsUsecase authenticateCredentialsUsecase;

//code

  Stream<LoginState> mapEventToState(
    LoginEvent event,
  ) async* {
        //code
        authenticationBloc.add(AuthenticationLogin());
        yield LoginLoadSuccess();
        //code
  }
}

What I'm trying to accomplish:

class _AppViewState extends State<AppView> {

  final _navigatorKey = GlobalKey<NavigatorState>();
  NavigatorState get _navigator => _navigatorKey.currentState;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      navigatorKey: _navigatorKey,
      builder: (context, child) {
        return BlocListener<AuthenticationBloc, AuthenticationState>(
          listener: (context, state) {
             if (state is AuthenticationAuthenticated) {
                _navigator.pushAndRemoveUntil<void>(
                  HomePage.route(),
                  (route) => false,
                );
             }
             else if (state is AuthenticationUnauthenticated){
                _navigator.pushAndRemoveUntil<void>(
                  LoginScreen.route(),
                  (route) => false,
                );
             }
          },
          child: child,
        );
      },
      onGenerateRoute: (_) => SplashPage.route(),
    );
  }
}

As you can see, the user is currently in the LoginScreen, once the login is successful, I need to yield the AuthenticationAuthenticated() state in my AuthenticationBloc() so my users will be directed to the HomePage()

How can I yield the AuthenticationAuthenticated() state of the AuthenticationBloc() inside my LoginBloc() - since my login logic happens inside the LoginBloc.

2
  • Hi! It's not entirely clear. What do you want to do in the Authenticationlogin () event if the login is successful. Navigate to another page? Aug 18, 2020 at 10:48
  • I need to call the AuthenticationLogin() event so it can yield the new state - AuthenticationAuthenticated() and navigate to the HomePage(). I'll edit my question to make it clearer, thank you for pointing that out Aug 18, 2020 at 14:48

2 Answers 2

2
  1. I subscribed the AuthenticationBloc to the status stream of my AuthenticateCredentialsUsecase class.
  2. When the AuthenticateCredentialsUsecase is called in my LoginBloc and the credentials are authenticated...
  3. I then update the status stream - _controller.add(AuthenticationStatus.authenticated);
  4. Which inturn will trigger the AuthenticationLogin event inside the AuthenticationBloc

AuthenticationBloc

 AuthenticationBloc({
    @required CheckAuthenticationStatusUsecase checkAuthenticationStatus,
    @required LogoutAuthenticatedUserUsecase logoutAuthenticatedUser,
    @required AuthenticateCredentialsUsecase authenticateCredentials,
  })  : assert(checkAuthenticationStatus != null),
        assert(logoutAuthenticatedUser != null),
        assert(authenticateCredentials != null),
        checkAuthenticationStatusUsecase = checkAuthenticationStatus,
        logoutAuthenticatedUserUsecase = logoutAuthenticatedUser,
        authenticateCredentialsUsecase = authenticateCredentials,
        super(AuthenticationInitial()) {
    add(AuthenticationStatusRequested());
    _loginStatusSubscription =
        authenticateCredentialsUsecase.status.listen((event) {
      if (event == AuthenticationStatus.authenticated) {
        add(AuthenticationLogin());
      }
    });
  }

AuthenticateCredentialsUsecase

  final _controller = StreamController<AuthenticationStatus>();

  Stream<AuthenticationStatus> get status async* {
    yield AuthenticationStatus.unknown;
    yield* _controller.stream;
  }

  void dispose() => _controller.close();

  @override
  Future<Either<Failure, AuthenticatedUser>> call(AuthenticationParams params) async {

    final result = await repository.authenticateCredentials(params.userName, params.password);

    if(result is Right){
      _controller.add(AuthenticationStatus.authenticated);
    }

    return result;
  }
0

Here is one way to do it. You must call BlocBuilder to handles building the widget in response to new states.

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData(
        primarySwatch: Colors.blue,
        visualDensity: VisualDensity.adaptivePlatformDensity,
      ),
      debugShowCheckedModeBanner: false,
      home: BlocBuilder<AuthenticationBloc, AuthenticationState>(
        builder: (context, state) {
          //If the login is successful, show homepage
          if (state is AuthenticationAuthenticated) {
            return HomePage();
          }
          //If the login failed, show login screen
          if (state is AuthenticationUnauthenticated) {
            return LoginScreen();
          }
          //If the login is in process, show loading indicator
          if (state is AuthenticationInProgress) {
            return LoadingIndicator();
          }
          return SplashScreen();
        },
      ),
    );
  }
}

At first, the state is AuthenticationUnauthenticated and displays the login screen. If the login is successful then we display homepage, otherwise if it failed we will display LoginScreen.

class LoginBloc extends Bloc<LoginEvent, LoginState> {
  final AuthenticationBloc authenticationBloc;
  final AuthenticateCredentialsUsecase authenticateCredentialsUsecase;

//code

  Stream<LoginState> mapEventToState(
    LoginEvent event,
  ) async* {

    if(event is LoginButtonPressed) {
        // some logic code
        // eg. : final response = UserRepository.login(username: event.username, password: event.password);
            authenticationBloc.add(AuthenticationLogin());
            //code

    }
        
  }
}

And here is the AuthenticationBloc () code which will handle the authentication.

 class AuthenticationBloc extends Bloc<AuthenticationEvent, AuthenticationState>{
    
    //code
    
      Stream<AuthenticationState> mapEventToState(
        AuthenticationEvent event,
      ) async* {
          if(event is AuthenticationLogin) {
             yield AuthenticationInProgress();
             //code
             yield AuthenticationAuthenticated();
          }
    }
}
    
6
  • I do have a BlocBuilder in my parent widget. And I am already implementing the code that you have given, but my question is - how can I yield the AuthenticationAuthenticated() state of the AuthenticationBloc() inside my LoginBloc() - since my login logic happens inside the LoginBloc Aug 19, 2020 at 2:09
  • No, you cannot yield the AuthenticationAuthenticated () state of the AuthenticationBloc () inside your LoginBloc (). It's just AuthenticationBloc () which will manage the display of widgets (screen) from the state you have! From the LoginBloc (), you can emit an event to AuthenticationBloc () and the latter will yield a state AuthenticationAuthenticated () and will return the corresponding widget (here it is HomePage ()). I just updated my code Aug 19, 2020 at 8:25
  • alright, but if you check my code above, I've already done what you just said, but it's still not working (the widget is not updating) Aug 19, 2020 at 8:42
  • Are you able to share some more code so that I can properly solve your problem? Aug 19, 2020 at 8:55
  • hey, thank you for the help. I ditched my old implementation. Instead, I subscribed my AuthenticationBloc to the status stream of my AuthenticateCredentialsUsecase class and when the credentials are authenticated, I then update the status stream which inturn will trigger the AuthenticationLogin event inside the AuthenticationBloc Aug 27, 2020 at 9:54

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.