How does BlocProvider.value works with added Event

1,242

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

  1. create a wrapper widget
  2. create the body (the main widget to display results) Below is an image attached to explain this enter image description here

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)

Share:
1,242
Opeyemi Noah
Author by

Opeyemi Noah

Updated on December 29, 2022

Comments

  • Opeyemi Noah
    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).

    streamprovi

    • Mäddin
      Mäddin almost 3 years
      Your 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
      Opeyemi Noah almost 3 years
      What's the best and optimal way to handle this? @Mäddin
    • Mäddin
      Mäddin almost 3 years
      I would do this initialization in didChangeDependencies probably. But I don't know if this is best practice.