get<T extends Object> static method
Get a registered T
name
is the used when locating a single service but not an inherited model.
filter
is a custom function that receives a list of the names of all the registered objects of type T
and
returns a String? that specifies which name to select. For example, if the registry contained objects of type
BookPage
with names "Page 3", "Page 4", and "Page 5", and you wanted to get the first one found:
final BookPage firstLetter = Bilocator.get<GreekLetter>(filter: (pageNames) => pageNames[0]);
Implementation
static T get<T extends Object>({String? name, Filter? filter}) {
assert(name == null || filter == null,
'Bilocator.get failed. `name` or `filter` cannot both be non-null.');
final String? updatedName;
if (filter == null) {
updatedName = name;
if (!Bilocator.isRegistered<T>(name: name)) {
throw Exception(
'Bilocator tried to get an instance of type $T with name $name but it is not registered. Possible causes:\n'
' - Data was stored in the widget tree using `location: Location.tree` so not found in the registry. See the '
'documentation for Bilocator for more information.\n\n',
);
}
} else {
if (_registry[T] == null) {
throw Exception(
'Bilocator tried to get an instance of type $T none are registered. Possible causes:\n'
' - Data was stored in the widget tree using `location: Location.tree` so not found in the registry. See the '
'documentation for Bilocator for more information.\n\n',
);
}
updatedName = filter(_registry[T]!.keys.toList());
}
return _registry[T]![updatedName]!.lazyInitializer.instance as T;
}