Viewing version 3.0.0. Open the latest package page.
injector
3.0.0injector is a simple to use and lightweight dependency injection library for dart
Readme
Injector 💉 Build Status
Injector is a simple dependency injection lib for Dart.
It does not replace a complex dependency injection framework like Dagger, but it provides the basics that most apps need. Feature requests are welcomed!
Internally the injector is a singleton that stores instances and builders in a Map.
Use registerDependency<Type>() to register the dependency.
Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)
All instances are lazy loaded, meaning that at the time you request the dependency the instance is created.
Get your dependency using get<Type>().
Usage
A simple usage example:
// Register a dependency (Every time a new instance)
injector.registerDependency<Car>(() {
var engine = injector.get<Engine>();
var fuel = injector.get<Fuel>();
var driver = injector.get<Driver>();
return CarImpl(engine,fuel,driver);
});
// Register a singleton
injector.registerSingleton<Car>(() {
var engine = injector.get<Engine>();
var fuel = injector.get<Fuel>();
var driver = injector.get<Driver>();
return CarImpl(engine,fuel,driver);
});
// Use custom Factories by extending [Factory].
injector.register(CustomFactory(() => Engine()));
// Register your dependencies / singletons in the __main.dart__ file
Notice
If you have an issue with getting dependencies from the injector, check that imports of your dependencies classes are absolute.
Good: import 'package:<project_name>/some/package/<file_name>.dart';
Bad: import '../../some/package/<file_name>.dart';
Features and bugs
Please file feature requests and bugs at the issue tracker.
Changelog
3.0.0
- Rename
BuildertoDependencyBuilderby saibotma
2.0.0
- Stable release for null safety
1.0.9
- Add
getas shortcut forgetDependency - Add deprecated annotation to
getDependency - Some preparations for nullability
- [bug] Fixed the bug, that the library detects a wrong circular dependency (#25)
1.0.8
- Add travis ci badge to readme
- Reformat using dartfmt
1.0.7
- Refactor docs
1.0.6
- Add remove dependency method
- Add override dependencies property
1.0.5
- Breaking Changes! Injector is not a singleton.
You can use it like a singleton with the static instance
Injector.appInstance - You can register your dependencies with a name. But be careful,
you need always the generic type! It looks like
Injector.allInstance.registerDependency<UserService>((injector) => UserServiceImpl), dependencyName: "MyCustomName");
1.0.4
- Add some documentation for each class
1.0.3
- Change license from BSD-3 to Apache 2
1.0.2
- Upgrade analysis_options.yaml
- Add missing Cast ("as T")
- Add Test - Cycle dependencies
1.0.1
- Upgrade pubspec.yaml (Change dart sdk constraint)
- Update README.md
- Update Description
1.0.0
- Create package,
- Add Injector class
- Add unit tests for "Injector"
- Add example - How to deal with the injector / dependency injector
Installing
export PUB_HOSTED_URL=https://pub.mirror.p-hatan.ir
flutter pub add 'injector:3.0.0'pubspec.yaml
name: injector
description: injector is a simple to use and lightweight dependency injection library for dart
version: 3.0.0
homepage: https://github.com/tappeddev/injector
environment:
sdk: '>=2.12.0 <4.0.0'
dev_dependencies:
extra_pedantic: ^4.0.0
test: ^1.24.4
Version history
| Version | Published | Archive | SHA-256 |
|---|---|---|---|
| 0.0.1 | On demand | Download | 667271b7…fbf5d1 |
| 0.1.2 | On demand | Download | 02752ea0…853635 |
| 1.0.1 | On demand | Download | 2f2fdcac…8790be |
| 1.0.2 | On demand | Download | 9e99f1d5…b6dd32 |
| 1.0.3 | On demand | Download | aeedf31d…82b69f |
| 1.0.4 | On demand | Download | b5337e1c…12fca3 |
| 1.0.5 | On demand | Download | b0a1992e…c142c3 |
| 1.0.6 | On demand | Download | a4ce0d41…32a884 |
| 1.0.7 | On demand | Download | d4a89565…de94ca |
| 1.0.8 | On demand | Download | 89fdc9c5…7b0b39 |
| 1.0.9 | On demand | Download | e37323d1…002130 |
| 1.0.10-nullsafety.0 | On demand | Download | ec0d781a…7b6cc5 |
| 2.0.0 | On demand | Download | 2a683124…78e832 |
| 3.0.0 | Cached · Upstream SHA-256 matched | Download | ed389bed…433e4e |
| 4.0.0 | On demand | Download | d692c377…28a2e1 |
Dependencies
No direct dependencies declared.