Overview
Scope
Scope provides Inversion of Control using the dependency injection (DI) pattern for Dart applications.
Scope allows you to inject values into a scope and then 'use' those dependencies from any method (or constructor) called within that scope.
Scope is not a replacement for the likes of Provider. Provider does dependency injection for your BuildContext whilst Scope provides DI for your call stack.
For Java developers, Scope provides similar functionality to a thread-local variables
Authors:
Philipp Schiffmann philippschiffmann93@gmail.com
S. Brett Sutton
Scope is a reimagining of Philipp's zone_id package. All credit goes to Phillipp's original implementation without which Scope wouldn't exist.
Sponsored by OnePub
Help support Scope by supporting OnePub, the private Dart repository.
OnePub allows you to privately share Dart packages between your own projects or with colleagues.
Try it for free and publish your first private package in seconds.
Scope is available on pub.dev at:
This is most easily understood via an example:
We create a Scope within main and call the method a()
which calls b()
. Both a() and b() are within the declared scope and therefore have access to the injected value ageKey
.
To access an injected value you call the use
method.
Inject multiple values of the same type
Scope allows you to inject multiple values of the same type
Inject Single values and Sequences
You can inject values generated from a factory method as a single
value or a sequence
of values.
The difference between injecting a single
value and a value
is that the single
's factory method has access to all other values injected into the scope.
A sequence
generates a new value each time the use
method is called with its key.
Nesting
Scope also allows you to nest scopes to any level.
Last updated