Flutter: State management

Viewed 48

I've seen many tutorials praising Bloc State management, what is so special about it and should I learn it as a beginner? if not is there any beginner friendly state management technique?

2 Answers

I would suggest using Getx as I have been using it for 2 years and it's great. As it makes your code arrangement great. I used it when I was a beginner & never encountered a need to use any other state management.

BLoC/Cubit

BLoC is great for complex state management for complex apps. However, inside the BLoC library there's a simpler way of managing state that's called Cubit (Cubit is sort of a subset of BLoC). Cubit is largely the same as BLoC except:

  1. less boilerplate
  2. doesn't use 2-way streams

This renders it much easier to learn, and a fantastic stepping-stone into a full-out BLoC driven state management solution.

Currently, my team and I are building a very complex app, and we use the principle: use Cubit's, unless there's a specific reason to use a BLoC. This has worked well for us (85% of our app is run with Cubit, 15% with BLoC).


In relation to other state management techniques, most people are probably going to recommend Provider or Riverpods (Riverpods = Provider on steroids). They are easier to learn than Cubit/BLoC. Except, only for simple cases (a few page app). Once your app gets complex (authentication, feeds, api calls, etc.) a Cubit/BLoC-based architecture is going to scale better and be much cleaner.

Additionally, the most-used state management system for production-level Flutter apps is BLoC/Cubit. So, if you're looking for a marketable skill, I'd default to that.

Helpful links:

Example app:

Here's a simple 1-feature app I made as a proof of concept to show how Cubit specifically works

Conclusion:

Provider, GetX, Riverpods, etc. are all easier to learn and contain less boilerplate than BLoC, except they won't scale as well when your app gets more complex.

To help combat the boilerplate/complexity problem of BLoC, use Cubits instead of BLoCs in your design unless you have a specific need for BLoCs.

Related