invokeNativeMethod static method
Generic method to invoke any native method with standardized error handling.
method: The name of the native method to invoke.arguments: The arguments to pass to the native method (optional).timeout: Maximum time to wait for the method call (default: 30 seconds).- Returns: A Future with the result from the native method.
- Throws: OsmosException for any errors that occur during method invocation.
Implementation
static Future<dynamic> invokeNativeMethod(
String method, {
Map<String, dynamic>? arguments,
Duration timeout = const Duration(seconds: 30),
}) async {
try {
return await _channel.invokeMethod(method, arguments).timeout(
timeout,
onTimeout: () => throw OsmosException.fromPlatformException(
PlatformException(
code: OsmosErrorCodes.timeoutError.code,
message:
'Method call timed out after ${timeout.inSeconds} seconds',
details: 'Method: $method',
),
),
);
} catch (error) {
// Filter out coroutine cancellation errors from video progress tracking
// These are expected when ads are closed and should not be reported as errors
if (error is PlatformException) {
final errorMessage = error.message?.toLowerCase() ?? '';
final errorDetails = error.details?.toString().toLowerCase() ?? '';
if ((errorMessage.contains('progress tracking stopped') &&
errorMessage.contains('standalonecoroutine was cancelled')) ||
(errorDetails.contains('jobcancellationexception') &&
errorDetails.contains('standalonecoroutine was cancelled'))) {
// Silently ignore video progress tracking cancellation errors
return null;
}
}
throw OsmosException.fromError(error,
fallbackErrorCode: OsmosErrorCodes.methodChannelError);
}
}