Flutter Dependency Injection
Tags: discuss, dart, flutter, android
Published: 2019-09-07 09:41:21

---
title: Flutter Dependency Injection
published: true
description: My two cents about DI in Flutter
tags: discuss, dart, flutter, android
cover_image: https://miro.medium.com/max/1250/1*Yk8aoVl3OWk0-pyG8499Bg.png
---

_cover image [source](https://medium.com/apparence/use-dependency-injection-with-flutter-9c3ee7bf4d16)_

Lately I've been fascinated with code sharing between web and mobile. Obviously there are many ways one can write code to be shared among platform and the phrase 'code sharing' can imply quite a number of things. The one that intrigued me the most was with Flutter, AngularDart (I'll continue referring to this as ngDart) and the BLoC pattern. 

To demonstrate my findings I took the flutter pre built counter app and implemented it in ngDart. I did try to make it as identical as possible but I'm lazy and I wasn't very much focused on the aesthetics. [Check out the repo here](https://github.com/sduduzog/counter_example)

To run the flutter app, open the flutter project folder and either use your IDE shortcut or in the terminal, do 
`> flutter run`
To run the angular app, it's almost same thing, but the command is 
`> webdev serve`

You'll see that with ngDart, dependency injection framework is no issue as it's built in but with Flutter, I had to shop around for the solution that looks like it fits.

### Solution 1
[flutter_simple_dependency_injection](https://pub.dev/packages/flutter_simple_dependency_injection)
I found  this package from [the article](https://www.toptal.com/cross-platform/code-sharing-angular-dart-flutter-bloc) I used to start off my code sharing example. I wouldn't have made the project if I didn't think that article although very informative it was rather complex when it came to the example.

### Solution 2
[Googles inject.dart repo](https://github.com/google/inject.dart)
This seemed like the go to solution. Their README mentions charming fancy words like
> Compile-time dependency injection for Dart and Flutter, similar to Dagger.

I was hooked, especially because I had just ported my [open source android app](https://github.com/sduduzog/slim-launcher) to Dagger.
I immediately ported my example to use it instead, while doing so I couldn't throw off the bad feeling that came with using this package.
- It isn't on the dart package registry [pub.dev](https://pub.dev) so there's a funky way you must use to add it to your project.
- Their README has a note stating ***This is not an official Google or Dart team project.* while the repo is literally hosted by the Google org on github.
- The issue tracker is disabled.
That was all enough for me to look for another solution. I didn't even get to see this in action. I stopped in the middle of configuring it.

### Solution 3
[dependecies_flutter](https://pub.dev/packages/dependencies_flutter)
While porting to this, I immediately felt comfortable. It was modular and felt native to the flutter Widget system. The author's examples we're also very easy to follow, I didn't have to google for a medium article just to understand how to use it.

I hope I don't get to Solution 4 because Solution 3 chased me away but rather because of what benefits my projects will be getting from the migration instead.