Steven
Steven

Reputation: 35

Flutter: State management

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?

Upvotes: 0

Views: 1316

Answers (3)

Matthew Trent
Matthew Trent

Reputation: 3274

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 Cubits, unless there's a specific reason to use a BLoC. This has worked well for us (90% of our app is run with Cubit, 10% 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 (understanding this will help you a lot):

Here's a simple 1-feature app I made as a proof of concept to show how Cubit specifically works. Read the project's README.md for context (star it? 😉).

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.


New answer:

I recommend just using Trent (https://pub.dev/packages/trent) for state management. It's like BLoC, but simpler. Lots of examples in the README on GitHub. Full disclosure: I made it.

Upvotes: 7

hayatbangash55
hayatbangash55

Reputation: 197

GETX - State Management

I would suggest Getx as I have been using it for 3 years and it's incredible.

It is effortless to learn.

I never encountered a need to use any other state management.

Features provided by GETX

  • State management
  • Dependency Injection
  • Theming
  • Clean Structure
  • Internationalization
  • Validation / Utils

Documentation

Upvotes: 1

lomza
lomza

Reputation: 9726

Cubits are quite easy to understand and can be used in most of the Flutter projects. For bigger apps I would go for Riverpod. Having independent providers gives a lot of flexibility, as they can be used in different parts of the app and you can make any future, use case or repository a provider.

I have wrote a tutorial with Flutter app on how to write a List - Details app using cubits, hooks and local database with Hive.

Upvotes: 1

Related Questions