provideTokenOptional<T extends Object> method
Finds and returns an object instance provided for a token
.
Unlike provideToken, null
is returned if a provider is not found.
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
}
Implementation
@nonVirtual
T? provideTokenOptional<T extends Object>(OpaqueToken<T> token) {
return unsafeCast(get(token, null));
}