lite_ref_core 0.1.1 copy "lite_ref_core: ^0.1.1" to clipboard
lite_ref_core: ^0.1.1 copied to clipboard

A lightweight dependency injection package for dart and flutter 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: Doesn't use hashmaps to store instances so it's faster than all other DI libraries.

  • Safe: Uses top level variables so it's impossible to get a NOT_FOUND error.

  • Lightweight: Has no dependencies.

  • Simple: Easy to learn with a small API surface:

    • Create a singleton:

      final dbRef = Ref.singleton(() => 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):

      // overrideWith is marked as @visibleForTesting so this isn't really necessary.
      dbRef.freeze();
      
    • Create a transient instance (always return new instance):

      final dbRef = Ref.transient(() => Database());
      
      assert(dbRef.instance != dbRef.instance);
      
    • Create a singleton asynchronously:

      final dbRef = Ref.asyncSingleton(() 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;
      

Click here for a flutter example with testing. #

1
likes
150
pub points
7%
popularity

Publisher

verified publishernosy.dev

A lightweight dependency injection package for dart and flutter with support for overriding for testing.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

meta

More

Packages that depend on lite_ref_core