I'm trying to find a way to create a new instance of a bloc and have it reflected in the multibloc provider.
currently I have the following :
Scaffold(
appBar: AppBar(),
body: MultiBlocProvider(
providers: [
BlocProvider<BlocABloc>(
create: (BuildContext context) => _aBloc,
),
BlocProvider<BlocBBloc>(
create: (BuildContext context) => _bBloc,
),
]...
I'm then trying to create a new instance of BlocABloc and also BlocBBloc as :
generateNew(){
setState(() {
_aBloc = BlocABloc();
_bBloc = BlocBBloc();
});
}
I was expecting the build function to re-execute and new instance is used in the BlocProvider. However, I'm finding that the BlocBuilder is still getting states from the previous instance of the Blocs.
Is there a way to do handle this scenario ?
I don't know the reason why you want to do this. But you can force
BlocProvider
create method to be executed again by usingkey
. Here is an example how you can do it.