skip to Main Content

I’m providing bloc at top of all view to access at globally. When I’m doing logout and re-login without closing app, event not called because it is already initiate. Here, I want to refresh bloc on logout, so that FetchList and UpdateCount events got called when user logged In without closing app. How I can achieve this?

class DemoApp extends StatelessWidget {
  const DemoApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(create: (_) => AppBloc()..add(const UpdateFcmToken())),
        BlocProvider(
          create: (_) => getIt<ListBloc>()
            ..add(const FetchList())
            ..add(const UpdateCount()),
        ),
      ],
      child: const AppView(),
    );
  }
}

For workaround, I add those event at login as well, but it will cause multiple request for fresh login.

2

Answers


  1. What about linking the bloc method:

     ..add(const FetchList())
     ..add(const UpdateCount()),
    

    with the authentication stream that you’re using in your app, I mean instead of calling the methods manually when creating the bloc, what about doing only this:

    class DemoApp extends StatelessWidget {
      const DemoApp({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MultiBlocProvider(
          providers: [
            BlocProvider(create: (_) => AppBloc()..add(const UpdateFcmToken())),
            BlocProvider(
              create: (_) => getIt<ListBloc>(),
               ),
          ],
          child: const AppView(),
        );
      }
    }
    

    I assumethat you’re using Firebase for authentication, and so, you can do something like this:

       FirebaseAuth.instance.authStateChanges().listen((User? user) {
         if(user != null) {
            final yourBloc = // here you get the cubit either by direct access with   getIt<ListBloc>() of with context.read<ListBloc>(), based on your case.
         
          yourBloc
           ..add(const FetchList())
           ..add(const UpdateCount()),     } 
        }
    

    and so now, you won’t need to worry about doing it manually, since every time a new user with authenticate (user != null), the events will be emited.

    Login or Signup to reply.
  2. You can achieve this by listening for a logout event and then resetting the ListBloc. You can create a new event in your ListBloc for this purpose, say ResetListBloc, and call it when the user logs out. This will cause the Bloc to re-initialize and fetch the list and update count as required.

    First, add a new event called ResetListBloc in your ListBloc:

    // In your ListBloc file
    
    abstract class ListEvent extends Equatable {
      const ListEvent();
    
      @override
      List<Object> get props => [];
    }
    
    class FetchList extends ListEvent {}
    
    class UpdateCount extends ListEvent {}
    
    class ResetListBloc extends ListEvent {} // Add this event
    

    Now, modify your ListBloc to handle this event:

    // In your ListBloc file
    
    class ListBloc extends Bloc<ListEvent, ListState> {
      // ...
    
      @override
      Stream<ListState> mapEventToState(ListEvent event) async* {
        if (event is FetchList) {
          // ...
        } else if (event is UpdateCount) {
          // ...
        } else if (event is ResetListBloc) {
          yield* _mapResetListBlocToState(); // Add this line
        }
      }
    
      Stream<ListState> _mapResetListBlocToState() async* {
        // Perform any cleanup or reset required before fetching the list
        add(const FetchList());
        add(const UpdateCount());
      }
    }
    

    Finally, in your logout process, add the ResetListBloc event to your ListBloc:

    // In your logout function
    
    void _onLogout(BuildContext context) {
      // ...
      BlocProvider.of<ListBloc>(context).add(const ResetListBloc());
      // ...
    }
    

    By adding this event, you ensure that when the user logs out, the ListBloc is reset, and the FetchList and UpdateCount events are called when the user logs in again without closing the app.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search