diff --git a/packages/flutter/lib/services.dart b/packages/flutter/lib/services.dart index b6a29f605a..d8f857b804 100644 --- a/packages/flutter/lib/services.dart +++ b/packages/flutter/lib/services.dart @@ -23,7 +23,6 @@ export 'src/services/keyboard_maps.dart'; export 'src/services/message_codec.dart'; export 'src/services/message_codecs.dart'; export 'src/services/platform_channel.dart'; -export 'src/services/platform_messages.dart'; export 'src/services/platform_views.dart'; export 'src/services/raw_keyboard.dart'; export 'src/services/raw_keyboard_android.dart'; diff --git a/packages/flutter/lib/src/services/asset_bundle.dart b/packages/flutter/lib/src/services/asset_bundle.dart index 1a81fdd469..a115209029 100644 --- a/packages/flutter/lib/src/services/asset_bundle.dart +++ b/packages/flutter/lib/src/services/asset_bundle.dart @@ -10,7 +10,7 @@ import 'dart:typed_data'; import 'package:flutter/foundation.dart'; -import 'binary_messenger.dart'; +import 'binding.dart'; /// A collection of resources used by the application. /// @@ -222,7 +222,7 @@ class PlatformAssetBundle extends CachingAssetBundle { Future load(String key) async { final Uint8List encoded = utf8.encoder.convert(Uri(path: Uri.encodeFull(key)).path); final ByteData? asset = - await defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData()); + await ServicesBinding.instance!.defaultBinaryMessenger.send('flutter/assets', encoded.buffer.asByteData()); if (asset == null) throw FlutterError('Unable to load asset: $key'); return asset; diff --git a/packages/flutter/lib/src/services/binary_messenger.dart b/packages/flutter/lib/src/services/binary_messenger.dart index 7589b2b96e..cd41909bec 100644 --- a/packages/flutter/lib/src/services/binary_messenger.dart +++ b/packages/flutter/lib/src/services/binary_messenger.dart @@ -6,7 +6,6 @@ import 'dart:typed_data'; import 'dart:ui' as ui; -import 'package:flutter/foundation.dart'; import 'binding.dart'; /// A function which takes a platform message and asynchronously returns an encoded response. @@ -77,36 +76,3 @@ abstract class BinaryMessenger { /// `channel` is not set. bool checkMockMessageHandler(String channel, MessageHandler? handler); } - -/// The default instance of [BinaryMessenger]. -/// -/// This API has been deprecated in favor of [ServicesBinding.defaultBinaryMessenger]. -/// Please use [ServicesBinding.defaultBinaryMessenger] as the default -/// instance of [BinaryMessenger]. -/// -/// This is used to send messages from the application to the platform, and -/// keeps track of which handlers have been registered on each channel so -/// it may dispatch incoming messages to the registered handler. -@Deprecated( - 'Use ServicesBinding.instance.defaultBinaryMessenger instead. ' - 'This feature was deprecated after v1.6.5.' -) -BinaryMessenger get defaultBinaryMessenger { - assert(() { - if (ServicesBinding.instance == null) { - throw FlutterError( - 'ServicesBinding.defaultBinaryMessenger was accessed before the ' - 'binding was initialized.\n' - "If you're running an application and need to access the binary " - 'messenger before `runApp()` has been called (for example, during ' - 'plugin initialization), then you need to explicitly call the ' - '`WidgetsFlutterBinding.ensureInitialized()` first.\n' - "If you're running a test, you can call the " - '`TestWidgetsFlutterBinding.ensureInitialized()` as the first line in ' - "your test's `main()` method to initialize the binding." - ); - } - return true; - }()); - return ServicesBinding.instance!.defaultBinaryMessenger; -} diff --git a/packages/flutter/lib/src/services/platform_channel.dart b/packages/flutter/lib/src/services/platform_channel.dart index 18a5a93810..3f176c3929 100644 --- a/packages/flutter/lib/src/services/platform_channel.dart +++ b/packages/flutter/lib/src/services/platform_channel.dart @@ -45,7 +45,7 @@ class BasicMessageChannel { final MessageCodec codec; /// The messenger which sends the bytes for this channel, not null. - BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; + BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger; final BinaryMessenger? _binaryMessenger; /// Sends the specified [message] to the platform plugins on this channel. @@ -139,7 +139,7 @@ class MethodChannel { /// The messenger used by this channel to send platform messages. /// /// The messenger may not be null. - BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; + BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger; final BinaryMessenger? _binaryMessenger; @optionalTypeArgs @@ -509,7 +509,7 @@ class EventChannel { final MethodCodec codec; /// The messenger used by this channel to send platform messages, not null. - BinaryMessenger get binaryMessenger => _binaryMessenger ?? defaultBinaryMessenger; + BinaryMessenger get binaryMessenger => _binaryMessenger ?? ServicesBinding.instance!.defaultBinaryMessenger; final BinaryMessenger? _binaryMessenger; /// Sets up a broadcast stream for receiving events on this channel. diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart deleted file mode 100644 index 813309a562..0000000000 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ /dev/null @@ -1,106 +0,0 @@ -// Copyright 2014 The Flutter Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - - -import 'dart:typed_data'; -import 'dart:ui' as ui; - -import 'binary_messenger.dart'; -import 'platform_channel.dart'; - -/// Sends binary messages to and receives binary messages from platform plugins. -/// -/// This class has been deprecated in favor of [defaultBinaryMessenger]. New -/// code should not use [BinaryMessages]. -/// -/// See also: -/// -/// * [BinaryMessenger], the interface which has replaced this class. -/// * [BasicMessageChannel], which provides basic messaging services similar to -/// `BinaryMessages`, but with pluggable message codecs in support of sending -/// strings or semi-structured messages. -/// * [MethodChannel], which provides platform communication using asynchronous -/// method calls. -/// * [EventChannel], which provides platform communication using event streams. -/// * -@Deprecated( - 'This class, which was just a collection of static methods, has been ' - 'deprecated in favor of BinaryMessenger, and its default implementation, ' - 'defaultBinaryMessenger. ' - 'This feature was deprecated after v1.6.5.' -) -class BinaryMessages { - // This class is not meant to be instantiated or extended; this constructor - // prevents instantiation and extension. - // ignore: unused_element - BinaryMessages._(); - - /// The messenger which sends the platform messages, not null. - static final BinaryMessenger _binaryMessenger = defaultBinaryMessenger; - - /// Calls the handler registered for the given channel. - /// - /// Typically called by [ServicesBinding] to handle platform messages received - /// from [dart:ui.PlatformDispatcher.onPlatformMessage]. - /// - /// To register a handler for a given message channel, see [setMessageHandler]. - @Deprecated( - 'Use defaultBinaryMessenger.handlePlatformMessage instead. ' - 'This feature was deprecated after v1.6.5.' - ) - static Future handlePlatformMessage( - String channel, - ByteData data, - ui.PlatformMessageResponseCallback callback, - ) { - return _binaryMessenger.handlePlatformMessage(channel, data, callback); - } - - /// Send a binary message to the platform plugins on the given channel. - /// - /// Returns a [Future] which completes to the received response, undecoded, in - /// binary form. - @Deprecated( - 'Use defaultBinaryMessenger.send instead. ' - 'This feature was deprecated after v1.6.5.' - ) - static Future? send(String channel, ByteData? message) { - return _binaryMessenger.send(channel, message); - } - - /// Set a callback for receiving messages from the platform plugins on the - /// given channel, without decoding them. - /// - /// The given callback will replace the currently registered callback for that - /// channel, if any. To remove the handler, pass null as the `handler` - /// argument. - /// - /// The handler's return value, if non-null, is sent as a response, unencoded. - @Deprecated( - 'Use defaultBinaryMessenger.setMessageHandler instead. ' - 'This feature was deprecated after v1.6.5.' - ) - static void setMessageHandler(String channel, Future Function(ByteData? message) handler) { - _binaryMessenger.setMessageHandler(channel, handler); - } - - /// Set a mock callback for intercepting messages from the `send*` methods on - /// this class, on the given channel, without decoding them. - /// - /// The given callback will replace the currently registered mock callback for - /// that channel, if any. To remove the mock handler, pass null as the - /// `handler` argument. - /// - /// The handler's return value, if non-null, is used as a response, unencoded. - /// - /// This is intended for testing. Messages intercepted in this manner are not - /// sent to platform plugins. - @Deprecated( - 'Use defaultBinaryMessenger.setMockMessageHandler instead. ' - 'This feature was deprecated after v1.6.5.' - ) - static void setMockMessageHandler(String channel, Future Function(ByteData? message) handler) { - _binaryMessenger.setMockMessageHandler(channel, handler); - } -}