mirror of
https://github.com/hanskokx/arcane_framework.git
synced 2026-05-14 10:29:06 +02:00
Added requiredServiceOfType getter on ArcaneServiceProvider
Signed-off-by: Hans Kokx <hans.d.kokx@gmail.com>
This commit is contained in:
@@ -77,6 +77,27 @@ class ArcaneServiceProvider
|
|||||||
return provider.registeredServices.whereType<T>().firstOrNull;
|
return provider.registeredServices.whereType<T>().firstOrNull;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Retrieves a service of type `T` from the nearest provider.
|
||||||
|
///
|
||||||
|
/// Throws an exception if no service of type `T` is found or if no provider exists.
|
||||||
|
///
|
||||||
|
/// Example:
|
||||||
|
/// ```dart
|
||||||
|
/// final myService = ArcaneServiceProvider.of<MyService>(context);
|
||||||
|
/// ```
|
||||||
|
static T requiredServiceOfType<T extends ArcaneService>(
|
||||||
|
BuildContext context,
|
||||||
|
) {
|
||||||
|
final provider = maybeOf(context);
|
||||||
|
assert(provider != null, "No ArcaneServiceProvider found");
|
||||||
|
|
||||||
|
final T? service = provider!.registeredServices.whereType<T>().firstOrNull;
|
||||||
|
|
||||||
|
assert(service != null, "No service of type $T found");
|
||||||
|
|
||||||
|
return service!;
|
||||||
|
}
|
||||||
|
|
||||||
/// Updates the service instances in this provider.
|
/// Updates the service instances in this provider.
|
||||||
///
|
///
|
||||||
/// This will trigger a rebuild of all widgets that depend on this provider.
|
/// This will trigger a rebuild of all widgets that depend on this provider.
|
||||||
|
|||||||
Reference in New Issue
Block a user