simple_ref 1.0.1 copy "simple_ref: ^1.0.1" to clipboard
simple_ref: ^1.0.1 copied to clipboard

A simple, lightweight and auto disposable service locator reference library with overrides support for Flutter.

Pub Star on Github License: MIT


Simple Ref #

A simple, lightweight and auto disposable service locator reference library with overrides support for Flutter.

Installation #

flutter pub add simple_ref
copied to clipboard

Usage #

Import package: #

import 'package:simple_ref.dart';
copied to clipboard

Singleton References: #

final repositoryRef = Ref(() => Repository());

// or with tear-off
final repositoryRef = Ref(Repository.new);

// Access the instance
final repository = repositoryRef();

// Override
repositoryRef.overrideWith(() => MockRepository());
copied to clipboard

For AutoDispose, wrap your app with a RefScope: #

runApp(
    const RefScope(
        child: MyApp(),
    ),
);
copied to clipboard

Implement, Extend or Mixin a Disposable and override the dispose method: #

class Controller implements Disposable {
  final counter = ValueNotifier(0);

  @override
  void dispose() {
    counter.dispose();
  }
}
copied to clipboard

Create a AutoDisposeRef: #

final controllerRef = Ref.autoDispose((_) => Controller());
copied to clipboard

Access the instance: #

This can be done in a Widget or in a Ref by using controllerRef.of(context) or controllerRef(context).

class CounterPage extends StatelessWidget {
    const CounterPage({super.key});

    @override
    Widget build(BuildContext context) {
        final controller = controllerRef.of(context);
        return Text('${controller.counter.value}');
    }
}
copied to clipboard

Override it: #

You can override the instance by using overrideWith. This is useful for testing or for initiate the async instances. In the example below, all calls to controllerRef.of(context) will return MockController.

controllerRef.overrideWith((context) => MockController());
copied to clipboard
2
likes
160
points
25
downloads

Publisher

verified publishereronsoft.com

Weekly Downloads

2024.10.02 - 2025.04.16

A simple, lightweight and auto disposable service locator reference library with overrides support for Flutter.

Homepage
Repository (GitHub)

Topics

#service-locator #dependency-injection #reference #ref #testable

Documentation

API reference

License

MIT (license)

Dependencies

flutter, ref_core

More

Packages that depend on simple_ref