lite_ref 0.2.1
lite_ref: ^0.2.1 copied to clipboard
A lightweight dependency injection package with support for overriding for testing.
Overview #
Lite Ref is a lightweight dependency injection library for Dart and Flutter.
Installation #
dart pub add lite_ref
Why Lite Ref? #
-
Fast: Lite Ref doesn't use hashmaps to store instances so it's faster than all other DI libraries.
-
Safe: Lite Ref uses top level variables so it's impossible to get a NOT_FOUND error.
-
Lightweight: Lite Ref has no dependencies.
-
Simple: Lite Ref is simple to use and has a small API surface:
-
Create a singleton:
final dbRef = Ref.singleton(create: () => Database()); assert(dbRef.instance == dbRef.instance); -
Use it:
final db = dbRef.instance; // or dbRef() -
Override it (for testing):
dbRef.overrideWith(() => MockDatabase()); -
Freeze it (disable overriding):
dbRef.freeze(); -
Create a transient instance (always return new instance):
final dbRef = Ref.transient(create: () => Database()); assert(dbRef.instance != dbRef.instance); -
Create a singleton asynchronously:
final dbRef = Ref.asyncSingleton(create: () async => await Database.init()); -
Use it:
final db = await dbRef.instance; -
Use it synchronously:
// only use this if you know the instance is already created final db = dbRef.assertInstance;
-