Remove redundant OptionalMethodChannel methods (#81930)
This commit is contained in:
@@ -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<T?> _invokeMethod<T>(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<dynamic>
|
||||
/// cannot masquerade as a List<T>. 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<dynamic>` cannot
|
||||
/// masquerade as a `List<T>`. 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<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>();
|
||||
}
|
||||
|
||||
/// An implementation of [invokeMethod] that can return typed maps.
|
||||
///
|
||||
/// Dart generics are reified, meaning that an untyped Map<dynamic, dynamic>
|
||||
/// cannot masquerade as a Map<K, V>. Since invokeMethod can only return
|
||||
/// Dart generics are reified, meaning that an untyped `Map<dynamic, dynamic>`
|
||||
/// cannot masquerade as a `Map<K, V>`. 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<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>();
|
||||
}
|
||||
|
||||
@@ -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<T?> invokeMethod<T>(String method, [ dynamic arguments ]) async {
|
||||
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.
|
||||
|
||||
Reference in New Issue
Block a user