Flutter Dependency Injection

sduduzog

Sdu

Posted on September 7, 2019

Flutter Dependency Injection

cover image source

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

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
I found this package from the article 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
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 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 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
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.

💖 💪 🙅 🚩
sduduzog
Sdu

Posted on September 7, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Flutter Dependency Injection
discuss Flutter Dependency Injection

September 7, 2019