How to use dependecy injection in flutter? How to use dependecy injection in flutter? dart dart

How to use dependecy injection in flutter?


There is a Flutter-compatible dependency injection framework that Google recently open-sourced. It is available here: https://github.com/google/inject.dart

This project provides static compile-time dependency injection, rather than relying on reflection to inject dependencies at run-time. If you are familiar with Dagger, it appears to be quite similar.

It's worth noting - this is not an official Google or Dart team project. At the time of writing, there is little documentation and it is currently considered a developer preview.


Angulars DI package can't be used independent of Angular.

The di package is quite outdated and depends on dart:mirrors which isn't available in Flutter

There seems to be a new DI package work in progress which is supposed to replace Angulars built-in DI and should also be useable standalone in Flutter or server-side applications, but there is no pre-release or source code available yet.

update

The announced DI package was delayed indefinitely.


If you want to use a class as a Singleton, from what I saw, the common usecase is to use a factory constructor.

How do you build a Singleton in Dart?

Never had to use any dependency injection system with Flutter for now.