56

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.

5 Answers 5

48

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.

6
  • Interesting... How is #1 different than #4? Commented Jul 23, 2019 at 18:24
  • 1
    Preventing circular dependency could be achieved without the model being scoped to a tree. For example, we could have a completely new kind of tree, made only for providers. Similarly, for #1 this also includes trying to obtain a value inside initState and deactivate, which may throw depending on what you do. Commented Jul 23, 2019 at 18:31
  • I meant #4, "override some values for only a specific widget tree", but I understand now with that example. Thanks! Commented Jul 23, 2019 at 18:35
  • 2
    @Rémi-Rousselet So as the developer of Provider, can you give some insight on when using Get It over provider would be preferred?
    – Ali80
    Commented Feb 23, 2020 at 21:23
  • 4
    This is too one-sided. get_it is simpler and can be used without a context. It also doesn't clutter your widget tree with Providers for services. So I think it's best to use both, but for different things.
    – EzPizza
    Commented Feb 8, 2022 at 17:23
7

I am explaining just one limitation which I practically found, there may be others too.

After searching many tutorials and topics on Get_it that why people use Get_it() even we have dependency injection in the provider, I was unable to understand the difference in terms of DI. Then I stuck in a Scenario and find the answer to your question that "what are the limitations".

Are there any more differences and limitations between them?.

Scenario:

I had Nested widgets, Widget A has Widget B and Widget B has Widget C, I was using the provider and was accessing values in each widget whenever value changed. It was great, Then I make a new widget D which was a separate widget, it was not inside the widget A hierarchy. But when I try to access the same value in Widget D it was not changing. Because widget D is not in the tree of Widget A. Now here comes the limitation of dependency injection of the provider.

Conclusion

You will use Get_it to access values out of the tree widget. But you can't access the updated value using provider

Updated Answer

In the above scenario, you need to wrap the app with Provider to access all dependencies.

1
  • 4
    Yes, provider works with a widget tree, but most of the times you can inject objects with Provider at the root of your tree so all of your app can read those dependencies. Commented Sep 2, 2020 at 22:27
1

Get It is not a dependency injection solution but a service locator.

It's useful if you want to rapidly switch between two or more implementations of a class. For example to mockup a service, and change between the "real" service or the fake one (for debugging purpose).

Indeed it can't retrieve/supply reference to an existing object (exception is for singleton, but you can do the same by yourself without much more effort) and can supply only new objects.

1

from streaming through various tutorials what I learned is that the get it package can be called a global variable that can be accessed from any widget to any widget whether nested or not nested VS provider which can only be accessed between the nested widget. The example is better explained by M.ArslanKhan

1

In my experience the limitation using Provider is the need for Context. GetIt doesn't have that need. It can really become a headbanger. That's why Provider 2.0 AKA Riverpod was invented.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.