ditclear / dartin

A pragmatic lightweight dependency injection framework for Flutter developers.
https://medium.com/@ditclear/dartin-a-pragmatic-lightweight-dependency-injection-framework-for-flutter-developers-bbb57c47b31f
BSD 3-Clause "New" or "Revised" License
43 stars 4 forks source link
dart dependency-injection flutter

dartin

A pragmatic lightweight dependency injection framework for Flutter developers.

A light container inspired by flutter-provide and koin.

more examples see mvvm_flutter.

Setup

dependencies:
  dartin: ^0.1.5

Key methods

more informations see dartin.dart.

Getting Started

  1. declare dartin modules

//scope should always be initialized as a static const and passed around.
const test = DartInScope('test');

final viewModelModule = Module([
  factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get<GithubRepo>())),
])
  ..withScope(test , [
    ///other scope
   factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get<GithubRepo>())),
  ]);

final repoModule = Module([
  single<GithubRepo>(({params}) => GithubRepo(get<GithubService>())),
]);

final remoteModule = Module([
  single<GithubService>(({params}) => GithubService()),
]);

final appModule = [viewModelModule, repoModule, remoteModule];
  1. Start dartin

void main() {
  startDartIn(appModule);
  runApp(MyApp());
}
  1. inject

//default
final service = inject<GithubService>();
//pass parameters
final test = inject<HomeProvide>(params: ['title']);
//different scope
final test = inject<HomeProvide>(scope:test,params: ['title']);

DartIn is easy to use, and still improving.

🍺 welcome to fork and pull request。

Article

【Medium】:DartIn:A pragmatic lightweight dependency injection framework for Flutter developers

【简书】:为Flutter而生的轻量级依赖检索容器—DartIn

【掘金】:为Flutter而生的轻量级依赖检索容器—DartIn

Licence

BSD