13

i am using flutter_bloc, and i am wondering which method should i use and what is the difference between these two ways?: i read that the first one with (value) the bloc will not automatically closed, but actually i don't understand what is mean?

BlocProvider<LoginBloc>.value(
  value:  (LoginBloc(LoginInitialState(), AuthRepository())),
  ),

  

 BlocProvider<ProfileBloc>(
         create:  (context) => ProfileBloc(ProfileInitialState(), AuthRepository()),
       ),
Osama Mohammed
  • 2,433
  • 13
  • 29
  • 61
  • Even if I've created a quite large app with flutter + Bloc. I tend often to get back to what is actually best practices with Bloc. When to use BlocProvider.value and/or BlocProvider(create:). If I figure something out I will let you know. – Dynamic_Pace Dec 11 '20 at 14:23

6 Answers6

20

As far as I understand it, you would use:

BlocProvider.value(
  value: BlocProvider.of<BlocA>(context),
  child: ScreenA(),
);

when you have already created a bloc in a different BlocProvider and you just want that same bloc to be available somewhere else in the widget tree.

I'm assuming that because this bloc wasn't created by the BlocProvider you're currently using (with BlocProvider.value) it won't handle closing the bloc - that will be done by the original BlocProvider.

So unless the bloc that you want to use doesn't exist somewhere else already, you can probably just use the normal method with create.

matkv
  • 652
  • 1
  • 7
  • 18
  • 1
    thank you for your reply, but still not clear, because i used to create all blocs in the Main function , and use them either by BlocBuilder() or BlocProvider.of(), so i don't understand your idea ? – Osama Mohammed Oct 22 '20 at 10:59
  • @OsamaMohammed I don't really understand what your question is exactly then? – matkv Oct 22 '20 at 11:09
  • all of examle has same i still stuck on how ScreenA() print to the UI of value dammmnn i didnt find it for example ScreenA(child: Text('how to print the value shit')) haha confused – Yogi Arif Widodo Apr 07 '22 at 21:46
  • Maybe reword "when you have already created a bloc in a different BlocProvider and you just want that same bloc to be available somewhere else in the widget tree." to "when you have already created a bloc in a different BlocProvider and you just want that same bloc to be available somewhere in a different widget tree." If you are using the same widget tree, you could just raise the bloc provider or read said bloc provider off of the context lower in the widget tree. – David Chopin Mar 28 '23 at 18:31
9

In our case, if we're creating a brand new cubit just to pass into the child, we'll use:

BlocProvider<NameOfCubit>(
  ...
  child: Screen(),
)

and if we want to use a cubit we've already created then we'll pass it though with:

BlocProvider<NameOfCubit>.value(
  ...
  child: Screen(),
)
Lucas
  • 329
  • 2
  • 8
1

I am wondering which method should i use and what is the difference between these two ways?

You should use BlocProvider(create:) unless you don't need to pick and use an already existing Bloc instance.

From package:flutter_bloc/src/bloc_provider.dart:

A new [Bloc] or [Cubit] should not be created in BlocProvider.value. New instances should always be created using the default constructor within the [Create] function.

The following code is wrong, because you create the LoginBloc instance in the value:

BlocProvider<LoginBloc>.value(
  value:  (LoginBloc(LoginInitialState(), AuthRepository())),
  child: .....
  ),

but you can use this to get the LoginBloc instance from elsewhere up the context tree:

BlocProvider<LoginBloc>.value(
  value:  BlocProvider.of<LoginBloc>(context),
  child: ....
  ),
Mario Daglio
  • 108
  • 9
1

based on the official docs, BlocProvider should be used to create new blocs which will be made available to the rest of the subtree. In this case, since BlocProvider is responsible for creating the bloc, it will automatically handle closing it.

BlocProvider(
  create: (BuildContext context) => BlocA(),
  child: ChildA(),
);

In some cases, BlocProvider can be used to provide an existing bloc to a new portion of the widget tree. This will be most commonly used when an existing bloc needs to be made available to a new route. In this case, BlocProvider will not automatically close the bloc since it did not create it.

BlocProvider.value(
  value: BlocProvider.of<BlocA>(context),
  child: ScreenA(),
);

so you did the first method in the wrong way.

0

In my opinion, bloc provider will reset all your states and events, and send a new EventIntial again. on the other hand, Bloc.value won't send EventInitial again.

0

BlocProvider.value() won't close your bloc automatically and you need to close it manually by using dispose method.