Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dependecy injection in flutter?

I wan to use di. in flutter and I add this https://pub.dartlang.org/packages/di packages my project and I started to read this https://webdev.dartlang.org/angular/guide/dependency-injection article, but I don't fully understand.

So it's ok: use @Injectable() annotation on services class(e.g: MyServices), but how to inject other class? For example I would like to similar:

class MyClass{
   //some variable
   var asd = MyService.asd; //need inject var.
                            //maybe use injector.get(MyService).asd; 
                            //but how add injector? (I don't add across constructor)

   MyService.oneMethod;//need inject method
}

main(){
    var injector = new ModuleInjector([new Module()
       ..bind(MyService)
    ]);
}

The point is, I don't want to use a constructor. I want to directly use injector. This is possible in flutter/dart?

like image 392
ZeroProcess Avatar asked Oct 17 '25 03:10

ZeroProcess


1 Answers

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.

like image 126
HoppedUpDev Avatar answered Oct 19 '25 18:10

HoppedUpDev