What is Flutter?

Flutter is a cross-platform, high-fidelity, high-performance mobile application development framework launched by Google and open source. Dart allows developers to develop apps, a set of code that runs on both iOS and Android.

Website of Flutter: Flutter – IO

After learning About Flutter for some time, we have tried it out on real projects and the results are pretty good: a perfect UI, the same business logic, reduced cost of communication, HotReload, and so on are all reasons to continue working on Flutter

However, compared with Android/IOS and other stable and efficient development modes for many years, it is undoubtedly still in a new stage. While writing the Dart code, I tried to transfer my previous development experience to the Flutter project, which naturally included the idea of dependency injection, but found that the community did not yet provide such third-party dependencies, but there was a need for them. In Google’s Q& A about Flutter PHASE ii, Related issues were also mentioned.

I have accumulated this knowledge from previous articles that analyzed the principles of Koin dependent retrieval (when Koin hits ViewModel). In addition, I recently found that a slight improvement in flutter-provide for state management could be transformed into a dependency injection framework, so I started to modify this aspect.

DartIn

Inspired by Koin, a lightweight dependency retrieval container for Kotlin developers.

Usage is similar to Koin, with 261 lines of code (including comments) that are easy to use and understand.

Github address: github.com/ditclear/da…

Dartin has been uploaded to pub for easy integration with Flutter developers.

dependencies:
  dartin: ^ 0.1.0 from
Copy the code

Some key methods:

  • Single: Creates a provider that provides globally unique instances.
  • Lazy: Creates a provider and creates instances only when it is first used.
  • Factory: Creates a provider that provides a new instance each time.
  • Inject: Inject object

More information can be found at Dartin.dart.

Quick start

  1. Create the Module and configure dependencies
// Scope must be global and static immutable
const test = DartInScope('test');

Dependencies defined in the Module constructor are placed in the default _defaule scope
final viewModelModule = Module([
  factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get<GithubRepo>())), ]) .. addOthers(test , [///other scope
   factory<HomeProvide>(({params}) => HomeProvide(params.get(0), get<GithubRepo>())),
  ]);

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

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

final appModule = [viewModelModule, repoModule, remoteModule];
Copy the code

2. Load the configuration items to dartins

void main() {
  startDartIn(appModule);
  runApp(MyApp());
}
Copy the code
  1. Perform dependency retrieval injection instances
// Inject a simple instance
final service = inject<GithubService>();
// Dynamically provide parameters
final test = inject<HomeProvide>(params: ['title']);
// Provide different instances according to scope
final test = inject<HomeProvide>(scope:test, params: ['title']);
Copy the code

More examples can be found at github.com/ditclear/mv…

The principle is very simple. In general, it is a dependency container Map. The key is the configured dependency type and the value is the encapsulated layer Provider.

factory(({params}) => HomeProvide(params.get(0), get()))

The Key is the HomeProvider, and the value is a Provider returned by factory

When it comes time to get an instance, supply it with the runtimeType of the instance, using the provider.get () method based on single/lazy/factory, so it’s important to remember to write T.

Write in the last

DartIn is the product of standing on the shoulders giants, features, but there are still many compared to Koin the framework for further, including complete unit tests, type safety, more reasonable, rely on the build path, in the future I will continue to perfect it, also hope to be able to derive some, a dependency injection framework better at an early date.

Finally 🍺 welcome to Star, fork and pull Request.

= = = = = = = = = = = = = = = = = = = = = = = = = = line = = = = = = = = = = = = = = = = = = = = = = = = = =

If you want to learn more about MVVM, Flutter, and responsive programming, please follow me.

You can find me at:

Jane: www.jianshu.com/u/117f1cf0c…

The Denver nuggets: juejin. Cn/user / 817692…

Github: github.com/ditclear