diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index c9bad0e210..397384dc83 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -142,6 +142,25 @@ class MethodChannel { BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger; final BinaryMessenger? _binaryMessenger; + /// Backend implementation of [invokeMethod]. + /// + /// The `method` and `arguments` arguments are used to create a [MethodCall] + /// object that is passed to the [codec]'s [MethodCodec.encodeMethodCall] + /// method. The resulting message is then sent to the embedding using the + /// [binaryMessenger]'s [BinaryMessenger.send] method. + /// + /// If the result is null and `missingOk` is true, this returns null. (This is + /// the behaviour of [OptionalMethodChannel.invokeMethod].) + /// + /// If the result is null and `missingOk` is false, this throwsa + /// [MissingPluginException]. (This is the behaviour of + /// [MethodChannel.invokeMethod].) + /// + /// Otherwise, the result is decoded using the [codec]'s + /// [MethodCodec.decodeEnvelope] method. + /// + /// The `T` type argument is the expected return type. It is treated as + /// nullable. @optionalTypeArgs Future _invokeMethod(String method, { required bool missingOk, dynamic arguments }) async { assert(method != null); @@ -333,29 +352,29 @@ class MethodChannel { /// An implementation of [invokeMethod] that can return typed lists. /// - /// Dart generics are reified, meaning that an untyped List - /// cannot masquerade as a List. Since invokeMethod can only return - /// dynamic maps, we instead create a new typed list using [List.cast]. + /// Dart generics are reified, meaning that an untyped `List` cannot + /// masquerade as a `List`. Since [invokeMethod] can only return dynamic + /// maps, we instead create a new typed list using [List.cast]. /// /// See also: /// /// * [invokeMethod], which this call delegates to. Future?> invokeListMethod(String method, [ dynamic arguments ]) async { - final List? result = await invokeMethod?>(method, arguments); + final List? result = await invokeMethod>(method, arguments); return result?.cast(); } /// An implementation of [invokeMethod] that can return typed maps. /// - /// Dart generics are reified, meaning that an untyped Map - /// cannot masquerade as a Map. Since invokeMethod can only return + /// Dart generics are reified, meaning that an untyped `Map` + /// cannot masquerade as a `Map`. Since [invokeMethod] can only return /// dynamic maps, we instead create a new typed map using [Map.cast]. /// /// See also: /// /// * [invokeMethod], which this call delegates to. Future?> invokeMapMethod(String method, [ dynamic arguments ]) async { - final Map? result = await invokeMethod?>(method, arguments); + final Map? result = await invokeMethod>(method, arguments); return result?.cast(); } @@ -453,26 +472,13 @@ class MethodChannel { /// instead of throwing an exception. class OptionalMethodChannel extends MethodChannel { /// Creates a [MethodChannel] that ignores missing platform plugins. - const OptionalMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec()]) - : super(name, codec); + const OptionalMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger]) + : super(name, codec, binaryMessenger); @override Future invokeMethod(String method, [ dynamic arguments ]) async { return super._invokeMethod(method, missingOk: true, arguments: arguments); } - - @override - Future?> invokeListMethod(String method, [ dynamic arguments ]) async { - final List? result = await invokeMethod>(method, arguments); - return result?.cast(); - } - - @override - Future?> invokeMapMethod(String method, [ dynamic arguments ]) async { - final Map? result = await invokeMethod>(method, arguments); - return result?.cast(); - } - } /// A named channel for communicating with platform plugins using event streams.