Skip to content

Add a type argument <T> to invokeMethod #17333

@matanlurey

Description

@matanlurey

i.e. instead of:

Future<dynamic> invokeMethod(String method, [dynamic arguments]) async {
assert(method != null);
final dynamic result = await BinaryMessages.send(
name,
codec.encodeMethodCall(new MethodCall(method, arguments)),
);
if (result == null)
throw new MissingPluginException('No implementation found for method $method on channel $name');
return codec.decodeEnvelope(result);
}

Something like:

Future<T> invokeMethod<T>(String method, [dynamic arguments]) async {
  ...
  return result as T;
}

There are a lot of runtime errors being reported by errors:
dart-lang/pub-dev#1263 (comment)

The advantage here is that T can be filled in automatically in most cases:

// Automatically infers invokeMethod<bool>!
Future<bool> isEnabled() => invokeMethod('isEnabled');

... and I imagine this would be a non-breaking change, as a plus! This won't work for more complex types, like List<String>, but maybe that's either OK or some variants of invokeMethod could be added to help, for example:

  • Future<List<T>> invokeMethodList<T>(...)
  • Future<Map<K, V>> invokeMethodMap<K, V>(...)

Metadata

Metadata

Assignees

No one assigned

    Labels

    frameworkflutter/packages/flutter repository. See also f: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions