Unverified Commit 55354aa2 authored by Ian Hickson's avatar Ian Hickson Committed by GitHub

Remove redundant OptionalMethodChannel methods (#81930)

parent 58499c29
...@@ -142,6 +142,25 @@ class MethodChannel { ...@@ -142,6 +142,25 @@ class MethodChannel {
BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger; BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger;
final BinaryMessenger? _binaryMessenger; 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 @optionalTypeArgs
Future<T?> _invokeMethod<T>(String method, { required bool missingOk, dynamic arguments }) async { Future<T?> _invokeMethod<T>(String method, { required bool missingOk, dynamic arguments }) async {
assert(method != null); assert(method != null);
...@@ -333,29 +352,29 @@ class MethodChannel { ...@@ -333,29 +352,29 @@ class MethodChannel {
/// An implementation of [invokeMethod] that can return typed lists. /// An implementation of [invokeMethod] that can return typed lists.
/// ///
/// Dart generics are reified, meaning that an untyped List<dynamic> /// Dart generics are reified, meaning that an untyped `List<dynamic>` cannot
/// cannot masquerade as a List<T>. Since invokeMethod can only return /// masquerade as a `List<T>`. Since [invokeMethod] can only return dynamic
/// dynamic maps, we instead create a new typed list using [List.cast]. /// maps, we instead create a new typed list using [List.cast].
/// ///
/// See also: /// See also:
/// ///
/// * [invokeMethod], which this call delegates to. /// * [invokeMethod], which this call delegates to.
Future<List<T>?> invokeListMethod<T>(String method, [ dynamic arguments ]) async { Future<List<T>?> invokeListMethod<T>(String method, [ dynamic arguments ]) async {
final List<dynamic>? result = await invokeMethod<List<dynamic>?>(method, arguments); final List<dynamic>? result = await invokeMethod<List<dynamic>>(method, arguments);
return result?.cast<T>(); return result?.cast<T>();
} }
/// An implementation of [invokeMethod] that can return typed maps. /// An implementation of [invokeMethod] that can return typed maps.
/// ///
/// Dart generics are reified, meaning that an untyped Map<dynamic, dynamic> /// Dart generics are reified, meaning that an untyped `Map<dynamic, dynamic>`
/// cannot masquerade as a Map<K, V>. Since invokeMethod can only return /// cannot masquerade as a `Map<K, V>`. Since [invokeMethod] can only return
/// dynamic maps, we instead create a new typed map using [Map.cast]. /// dynamic maps, we instead create a new typed map using [Map.cast].
/// ///
/// See also: /// See also:
/// ///
/// * [invokeMethod], which this call delegates to. /// * [invokeMethod], which this call delegates to.
Future<Map<K, V>?> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async { Future<Map<K, V>?> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async {
final Map<dynamic, dynamic>? result = await invokeMethod<Map<dynamic, dynamic>?>(method, arguments); final Map<dynamic, dynamic>? result = await invokeMethod<Map<dynamic, dynamic>>(method, arguments);
return result?.cast<K, V>(); return result?.cast<K, V>();
} }
...@@ -453,26 +472,13 @@ class MethodChannel { ...@@ -453,26 +472,13 @@ class MethodChannel {
/// instead of throwing an exception. /// instead of throwing an exception.
class OptionalMethodChannel extends MethodChannel { class OptionalMethodChannel extends MethodChannel {
/// Creates a [MethodChannel] that ignores missing platform plugins. /// Creates a [MethodChannel] that ignores missing platform plugins.
const OptionalMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec()]) const OptionalMethodChannel(String name, [MethodCodec codec = const StandardMethodCodec(), BinaryMessenger? binaryMessenger])
: super(name, codec); : super(name, codec, binaryMessenger);
@override @override
Future<T?> invokeMethod<T>(String method, [ dynamic arguments ]) async { Future<T?> invokeMethod<T>(String method, [ dynamic arguments ]) async {
return super._invokeMethod<T>(method, missingOk: true, arguments: arguments); return super._invokeMethod<T>(method, missingOk: true, arguments: arguments);
} }
@override
Future<List<T>?> invokeListMethod<T>(String method, [ dynamic arguments ]) async {
final List<dynamic>? result = await invokeMethod<List<dynamic>>(method, arguments);
return result?.cast<T>();
}
@override
Future<Map<K, V>?> invokeMapMethod<K, V>(String method, [ dynamic arguments ]) async {
final Map<dynamic, dynamic>? result = await invokeMethod<Map<dynamic, dynamic>>(method, arguments);
return result?.cast<K, V>();
}
} }
/// A named channel for communicating with platform plugins using event streams. /// A named channel for communicating with platform plugins using event streams.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment