requireCapability<T, R> static method

Future<R> requireCapability<T, R>(
  1. dynamic provider,
  2. Future<R> action(
    1. T
    ), {
  3. String? capabilityName,
})

Execute action safely with error handling Throws CapabilityError if not supported

Implementation

static Future<R> requireCapability<T, R>(
  dynamic provider,
  Future<R> Function(T) action, {
  String? capabilityName,
}) async {
  if (provider is! T) {
    throw CapabilityError(
      'Provider does not support ${capabilityName ?? T.toString()}',
    );
  }
  return await action(provider);
}