What is the difference between Bloc & Provider? and which is the best?

A brief introduction to Flutter architecture

Choosing the architecture for a Flutter development project is of great importance, primarily due to the fact that we are dealing with a less commonly used, declarative programming paradigm. This completely changes the approach to managing the state that native Android or iOS developers were familiar with, writing the code imperatively. Data available in one place in the application are not so easy to obtain in another. We do not have direct references to other views in the tree, from which we could gain their current state.

What is BLoC in Flutter

Business Logic Components is a Flutter architecture much more similar to popular solutions in mobile such as MVP or MVVM. It provides separation of the presentation layer from business logic rules. This directly applies the declarative approach that Flutter strongly emphasizes, i.e. UI = f (state). BLoC is a place where events from the user interface go. Within this layer, due to applying business rules to a given event, BLoC responds with a specific state, which then goes back to the UI. When the view layer receives a new state, it rebuilds its view according to what the current state requires.

What is Provider in Flutter

As the name suggests, Provider is a Flutter architecture that provides the current data model to the place where we currently need it. It contains some data and notifies observers when a change occurs. In Flutter SDK, this type is called a ChangeNotifier. For the object of type ChangeNotifier to be available to other widgets, we need ChangeNotifierProvider. It provides observed objects for all of its descendants. The object which is able to receive current data is Consumer, which has a ChangeNotifier instance in the parameter of its build function that can be used to feed subsequent views with data.

Which State management system is best for flutter

The provider is the recommended choice by the flutter team, but it depends on what suits you. That's why there is a huge number of options. The most used ones that I know so far are BLoC and GetX. In small apps, I see Provider a lot.

  • so in my opinion, you should use Provider in small apps and Bloc in large apps.