I'm using Bloc pattern in a project and I'm getting confused on what way I should pass model to another screen.

Here's the scenario: User will Login. Get the credentials/values then pass it to the homescreen.

As of now, we're using the BLoC pattern to get the value after the user signed-in (will be stored in SigninState) then call the BlocBuilder to fetch that state/value from the SigninState to the new screen(home-screen).

What I have in my mind is that to refactor it by:
Process the values in SigninCubit.
if success, call SigninLoaded,
Then pass the User model as a parameter to the next screens so we don't have to call the BlocBuilder to access the user model in every screen's widget build function.

Any advantages and disadvantages?

Thank you.


Solution 1: Wilson Wilson

Passing the User model to the one screen may be simple at the moment, but it can easily get out of hand when you have tens or hundreds of screens and dialogs that require the user.

A better solution would be to use BlocProvider to provide the Bloc throughout your widget tree? That way, you can access the Bloc from anywhere and only rebuild parts of the tree that need it.

BlocProvider.value(
  value: SignInBloc(),
  child: MyApp(),
);

Then to build the parts of the tree that need it:

Widget build(BuildContext context){
   final state = context.watch<SignInBloc>().state;
   return Scaffold(
}