Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter : Is provider an alternative to the BLoC pattern?

I know that BLoC in flutter acts like the viewmodel layer in android's MVVM, so the data does not gets fetched again and again upon configuration changes (for ex: change in screen orientation).

I am confused if provider replaces the functionality of RxDart in BLoC pattern or it replaces the role BLoC pattern itself.

Also, if I don't use BLoC at all an only providers does the app survives configuration changes.

Please explain what are the limitations of provider over BLoC, RxDart combination with some use cases.

like image 375
curiousgeek Avatar asked Jun 10 '19 21:06

curiousgeek


People also ask

Is BLoC and provider the same?

So here, we can compare the StreamBuilder in Bloc with Consumer in Provider. The difference is that StreamBuilder listens to the stream and fetches the model on every change to rebuild the widget. But Consumer listens as soon as notifyListeners() executes inside the provider class.

Which is best BLoC or provider in Flutter?

It's depends on you. If you are a beginner, I would say start with provider. Then learn about Bloc.

What is Flutter BLoC provider?

BlocProvider is a flutter widget that creates and provides a Bloc to all of its children. This is known as a dependency injection widget, so that a single instance of Bloc can be provided to multiple widgets within a subtree.

Which is better GetX or provider?

It seems GetX is simpler than Provider and has more capabilities/features like navigation, but there are not a lot of codes/tutorials using that!


1 Answers

Provider in itself doesn't replace the BLoC pattern. However, you can set up your architecture to use Provider in a way that could replace the BLoC pattern.

One way to do that would be to use the MVVM pattern, where you have a view model class that extends a ChangeNotifier. Then you can listen to that view model with a ChangeNotifierProvider so that the UI gets rebuilt any time the view model changes. FilledStacks does that well here.

See also

  • A beginner’s guide to architecting a Flutter app
  • State Management With Provider

Personally I find it easier to use the builtin Flutter tools to manage state. I describe that more here:

  • Flutter state management for minimalists
like image 66
Suragch Avatar answered Oct 10 '22 11:10

Suragch