invokeAsynchronousMethod<T> method

Future<T> invokeAsynchronousMethod<T>(
  1. String method, [
  2. dynamic arguments
])

Invokes an asynchronous method on this channel with the specified arguments.

The static type of arguments is dynamic, but only values supported by the codec of this channel can be used. The same applies to the returned result. The values supported by the default codec and their platform-specific counterparts are documented with StandardMessageCodec.

The generic argument T of the method can be inferred by the surrounding context, or provided explicitly. If it does not match the returned type of the channel, a TypeError will be thrown at runtime. T cannot be a class with generics other than dynamic. For example, Map<String, String> is not supported but Map<dynamic, dynamic> or Map is.

Returns a Future which completes to one of the following:

  • a result (possibly null), on successful invocation;
  • a PlatformException, if the invocation failed in the platform plugin;
  • a MissingPluginException, if the method has not been implemented by a platform plugin.

Implementation

Future<T> invokeAsynchronousMethod<T>(String method, [arguments]) async {
  final jobId = generateId();
  final Completer<T?> job = Completer<T>();
  _jobs[jobId] = job;
  if (DEBUG) {
    print("$TAG method#$method,jobId#$jobId,arguments#$arguments");
    job.future.whenComplete(() async {
      print("$TAG method#$method,jobId#$jobId,future#${await job.future}");
    });
  }
  try {
    await _channel.invokeMethod<T>(method, {
      "__job_id": jobId,
      "__argument": arguments,
    }).timeout(Duration(seconds: timeout));
  } on TimeoutException catch (_) {
    throw StateError(
        "On the platform side, you must first call result.success(null) and then execute the asynchronous task.");
  }
  return job.future as FutureOr<T>;
}