flutter_simple_service_container 1.0.0-dev.2 copy "flutter_simple_service_container: ^1.0.0-dev.2" to clipboard
flutter_simple_service_container: ^1.0.0-dev.2 copied to clipboard

Provides extensions to simple_service_container that streamline its useage with flutter.

Provides extensions to simple_service_container that streamline its usage with flutter. Makes use of extension methods that use context_watch for watching listenable services.

Set-up #

Just add following dependencies to your pubspec.yaml:

dependencies:
  simple_service_container: ^1.0.0
  flutter_simple_service_container: ^1.0.0

Usage and Getting Started #

Since the extensions for watching services work primarily via context_watch we will need to wrap our app in a ContextWatch.root.

  runApp(ContextWatch.root(child: MyApp()));

We can then set up a ServiceScope to provide child widgets with access to services via their BuildContexts. We can also nest ServiceScopes to provide access to a different ServiceContainer within their scope and allow us to limit access to some services or even override them within the nested scope.

Widget build(BuildContext context) {
  final services = ServiceContainer();

  // Do some service setup...

  return ServiceScope(
      services: services,
      child: const MaterialApp(title: 'My App', home: MainMenu()),
  );
}

Widgets within a ServiceScope will then be able to obtain services in a few different ways.

We can obtain the entire (read-only) service container from the build context like this:

final services = context.services;

This is especially useful for creating sub-containers for nested scopes:

Widget build(BuildContext context) {
  final subScopeServices = ServiceContainer(context.services);

  // Do some service setup for the sub-container...

  return ServiceScope(
      services: subScopeServices,
      child: const SomeWidget(),
  );
}

We can obtain individual services from the context like this:

final service = context.get<MyService>();

We can also obtain and watch listenable services like this to have the context rebuild whenever they notify of changes:

final service = context.watch<MyListenableService>();

Or even obtain a specific value from the service and watch for changes to it like this so that the context only rebuilds if/when the value changes:

final value = context.watchOnly(
  (MyListenableService service) => service.someValue,
);

Extensions #

You may also wish to check out the following packages that also extend the functionality of simple_service_container:

0
likes
160
points
12
downloads

Publisher

verified publisherliamrobinson.co.uk

Weekly Downloads

Provides extensions to simple_service_container that streamline its useage with flutter.

Repository (GitHub)
View/report issues

Documentation

API reference

License

MIT (license)

Dependencies

context_watch, flutter, simple_service_container

More

Packages that depend on flutter_simple_service_container