How does BlocProvider.value works with added Event
I found a workaround for this, Using the approach stated above will make the LoadProductFromStream event get called even before getting to /opening the StreamproductPage().
To avoid this a simple and efficient approach could be used instead
- create a wrapper widget
- create the body (the main widget to display results) Below is an image attached to explain this
Wrapper widget is where the bloc will be created/initialized and where LoadProductFromStream() event will be added to the bloc
In conclusion
This way we only add LoadFromStream() event to bloc when we get to StreamproductPage
Takeaway
BlocProvider(create:(context)=> xyzBloc()) // can automatically close bloc
while BlocProvider.value() // doesn't close bloc automatically (i stand to be corrected)
Opeyemi Noah
Updated on December 29, 2022Comments
-
Opeyemi Noah over 1 year
What will happen if An event is added to the bloc being passed inside the BlocProvider.value?
Will the event gets added before or after i get to the page?
And Is Blocprovider.value an ideal way to avoid adding events to bloc in initState() method? I'm trying to avoid initstate as I'm listening to Stream of data from firebase (this event need to be called only once).
-
Mäddin almost 3 yearsYour add-method in BlocProvider.value will be called every build. It is not the optimal way. This add-event will be added after the current build.
-
Opeyemi Noah almost 3 yearsWhat's the best and optimal way to handle this? @Mäddin
-
Mäddin almost 3 yearsI would do this initialization in didChangeDependencies probably. But I don't know if this is best practice.
-