How to use dependecy injection in flutter?

8,288

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

Hello what about something like this? Very simple implementation, Injector itself is singleton and also added classes into it. Of course can be extended very easily. If you are looking for something more sophisticated check this package: https://pub.dartlang.org/packages/flutter_simple_dependency_injection

void main() {  
  Injector injector = Injector();
  injector.add(() => Person('Filip'));
  injector.add(() => City('New York'));

  Person person =  injector.get<Person>(); 
  City city =  injector.get<City>();

  print(person.name);
  print(city.name);
}

class Person {
  String name;

  Person(this.name);
}

class City {
  String name;

  City(this.name);
}


typedef T CreateInstanceFn<T>();

class Injector {
  static final Injector _singleton =  Injector._internal();
  final _factories = Map<String, dynamic>();

  factory Injector() {
    return _singleton;
  }

  Injector._internal();

  String _generateKey<T>(T type) {
    return '${type.toString()}_instance';
  }

  void add<T>(CreateInstanceFn<T> createInstance) {
    final typeKey = _generateKey(T);
    _factories[typeKey] = createInstance();
  }

  T get<T>() {
    final typeKey = _generateKey(T);
    T instance = _factories[typeKey];
    if (instance == null) {
      print('Cannot find instance for type $typeKey');
    }

    return instance;
  }
}
Share:
8,288
ZeroProcess
Author by

ZeroProcess

Updated on November 21, 2022

Comments

  • ZeroProcess
    ZeroProcess over 1 year

    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?