Flutter Difference between BlocBuilder and context.watch?

212 views Asked by At

I am using cubit as state management in my app. What's the difference between BlocBuilder and context.watch? Imho both react to a value change in the underlying Cubit / Bloc and trigger a UI build? I am not asking for a code implementation, but for a use case recommendation.

1

There are 1 answers

2
tomerpacific On BEST ANSWER

When comparing the two methods and looking at the documentation, you can infer that:

  • BlocBuilder handles building a widget in response to a new state
  • context.watch obtains a value from the nearest ancestor provider of type T or T?, and subscribe to the provider

There is obviously more here and I can recommend reading up on it in the documentation. I would assume that in places where context is not available to you, you would maybe opt to use BlocBuilder.

Furthermore, context.watch is the same as calling Provider.of(context) and also can only be called StatelessWidget.build and State.build.

Also, I found this GitHub issue with some relevant information.