MultiToken<T extends Object> class
A token representing multiple values of T
for dependency injection.
const usPresidents = const MultiToken<String>('usPresidents');
@Component(
selector: 'presidents-list',
providers: const [
const ValueProvider.forToken(usPresidents, 'George Washington'),
const ValueProvider.forToken(usPresidents, 'Abraham Lincoln'),
],
)
class PresidentsListComponent {
// Will be ['George Washington', 'Abraham Lincoln'].
final List<String> items;
PresidentsListComponent(@Inject(usPresidents) this.items);
}
The type T
is not required, but is recommended, otherwise it is dynamic
.
The only positional argument, uniqueName
, is used to determine uniqueness
of the token. That is, const MultiToken('SECRETS')
is identical to
const MultiToken('SECRETS')
in another library or package.
You may also sub-class MultiToken to create a "more unique" token:
class UsPresidents extends MultiToken<String> {
const UsPresidents();
}
const usPresidents = const UsPresidents();
@Component(
selector: 'presidents-list',
providers: const [
const ValueProvider.forToken(usPresidents, 'George Washington'),
const ValueProvider.forToken(usPresidents, 'Abraham Lincoln'),
],
)
class PresidentsListComponent {
// Will be ['George Washington', 'Abraham Lincoln'].
final List<String> items;
PresidentsListComponent(@Inject(usPresidents) this.items);
}
NOTE: The List<T>
returned by dependency injection is not guaranteed
to be identical across invocations - that is, a new List
instance is
created every time a provider backed by a MultiToken is used:
const usPresidents = MultiToken<String>('usPresidents');
void example(Injector i) {
final a = i.provideToken(usPresidents);
final b = i.provideToken(usPresidents);
print(identical(a, b)); // false
}
WARNING: It is not supported to create a non-const instance of this
class or sub-types of this class. Instances should only be created or
referenced using the const
operator.
- Inheritance
-
- Object
- OpaqueToken<
List< T> > - MultiToken
- Annotations
-
- @optionalTypeArgs
Constructors
- MultiToken([String uniqueName = ''])
-
const
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
override
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited