lite_ref 0.5.0 copy "lite_ref: ^0.5.0" to clipboard
lite_ref: ^0.5.0 copied to clipboard

A lightweight dependency injection package with support for overriding for testing.

0.5.0 #

  • [Feat] Add autodipose for ChangeNotifiers and any class that implements Disposable. These classes will be disposed when all the widgets that have access to the instance are unmounted. If providing a existing and you don't want the instance to be disposed set autoDispose to false.

    In the example below, the CounterController will be disposed when the CounterView is unmounted.

    class CounterController extends ChangeNotifier {
    var _count = 0;
    int get count => _count;
    
    void increment() {
        _count++;
        notifyListeners();
    }
    
    void decrement() {
        _count--;
        notifyListeners();
    }
    }
    
    final countControllerRef = Ref.scoped((ctx) => CounterController());
    
    class CounterView extends StatelessWidget {
    const CounterView({super.key});
    
    @override
    Widget build(BuildContext context) {
        final contoller = countControllerRef.of(context);
        return ListenableBuilder(
        listenable: contoller,
        builder: (context, snapshot) {
            return Text('${contoller.count}');
        },
        );
    }
    }
    

0.4.1 #

  • [Docs] Add documentation comments and examples

0.4.0 #

  • [Breaking] This now a flutter package

  • Add ScopedRef which is a ref that needs a context to access its instance

  • Add LiteRefScope which coupled withScopedRef is an alternative to Provider for classes that don't rebuild widgets.

    • Wrap your app or a subtree with a LiteRefScope:

      runApp(
      LiteRefScope(
          child: MyApp(),
      ),
      );
      
    • Create a ScopedRef.

      final settingsServiceRef = Ref.scoped((ctx) => SettingsService());
      
    • Access the instance in the current scope:

      This can be done in a widget by using settingsServiceRef.of(context) or settingsServiceRef(context).

      class SettingsPage extends StatelessWidget {
      const SettingsPage({super.key});
      
      @override
      Widget build(BuildContext context) {
          final settingsService = settingsServiceRef.of(context);
          return Text(settingsService.getThemeMode());
      }
      }
      
    • Override it for a subtree:

      You can override the instance for a subtree by using overrideWith. This is useful for testing. In the example below, all calls to settingsServiceRef.of(context) will return MockSettingsService.

      LiteRefScope(
          overrides: [
              settingsServiceRef.overrideWith((ctx) => MockSettingsService()),
          ]
          child: MyApp(),
          ),
      

0.3.0 #

  • Make the factory function non-nullable (improves performance and maintainability)

0.2.1 #

  • Internal performance improvements
  • fix minor bug where async singleton would not be replace when overridden

0.2.0 #

  • separate singleton and transient instantiation use: Ref.singleton, Ref.transient, Ref.asyncSingleton and Ref.asyncTransient
  • add assertInstance getter for synchronous access to a AsyncSingletonRef

0.1.0 #

  • prevent race condition when fetching async singleton
  • add assertInstance getter for synchronous access to a LiteAsyncRef
  • add .freeze() method which disables overriding

0.0.2 #

  • Update readme

0.0.1 #

  • Initial release
14
likes
0
pub points
69%
popularity

Publisher

verified publishernosy.dev

A lightweight dependency injection package with support for overriding for testing.

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

Dependencies

basic_interfaces, flutter

More

Packages that depend on lite_ref