Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Provider vs. Get_it

Searching for Dependency Injection solutions for Flutter, I found two awesome libraries: provider and get_it.

As far as I can see, provider has more boilerplate, but it fits really nicely with Flutter, allowing Consumer to rebuild parts of the Widget tree, once an injected value change.

get_it on the other hand is more straightforward, easier to use, and not dependant on Flutter, so can be used with any Dart code.

Are there any more differences and limitations between them? I know this is kinda opinionated, but Flutter is so new that it's good to register publicly benefits, side-effects and gotchas.

like image 549
Michel Feinstein Avatar asked Jul 23 '19 17:07

Michel Feinstein


People also ask

Should I use provider or Riverpod?

Provider package is certainly a great tool, however, Riverpod has extended its capacity. In the flutter community, many developers think Riverpod is better than Provider, which is an earlier-released-state-management package.

What is a provider in Flutter?

The provider package is an easy to use package which is basically a wrapper around the InheritedWidgets that makes it easier to use and manage. It provides a state management technique that is used for managing a piece of data around the app.

What is Riverpod?

Riverpod is a popular Flutter state management library that shares many of the advantages of Provider and brings many additional benefits. According to the official documentation: Riverpod is a complete rewrite of the Provider package to make improvements that would be otherwise impossible.

Why do we use Get it in Flutter?

GetIt is a service locator that allows you to create interfaces and their implementations, and access those implementations globally, anywhere in your app. Injectable generates code that we would have otherwise written by using annotations.


1 Answers

The main difference between both is that provider is not strictly dependency injection.

By using widgets, provider is also able to:

  • providers are compatible with the Flutter devtool
  • know when a variable cannot be accessed (scoped to a tree)
  • know when to create and dispose of an object
  • synchronize model -> model and model -> UI
  • override some values for only a specific widget tree
  • voluntarily prevent circular dependency

All of these are, while optional, good for the health of your app in the long run.

It ensures that you're always up to date, makes it harder to have "spaghetti code", and makes your different elements more composable.

like image 79
Rémi Rousselet Avatar answered Oct 16 '22 09:10

Rémi Rousselet