-
Notifications
You must be signed in to change notification settings - Fork 29.7k
Description
Recently there was a breaking change:
#37489 proposed to move the default BinaryMessenger instance to ServicesBinding, and deprecate the former. This is going to be a breaking change, if users reference to the defaultBinaryMessenger directly. Instead, users will use ServicesBinding.instance.defaultBinaryMessenger to access the default BinaryMessenger that's used for sending platform messages.
Why this change?
By this change, we are capable of registering a different default BinaryMessenger under testing environment, by creating a ServicesBinding subclass for testing. With that, we can track # of pending platform messages for synchronization purposes. See more details in #37409.Path to migrate
You'll use ServicesBinding.instance.defaultBinaryMessenger to access the default BinaryMessenger. In your tests, make sure the ServicesBinding is properly initialized before accessing any message channels.If you have any concerns or otherwise feel that this change should not be made, please comment on the above PR or on the associated issue: #37409 -- or just reply to this email.
Thanks,
Ada
The Flutter plugin template needs to be updated.
Steps to reproduce:
flutter create --template plugin myplugin
cd myplugin
flutter test
Fails with:
00:06 +0 -1: getPlatformVersion [E]
ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
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.
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.
package:flutter/src/services/binary_messenger.dart 73:7 defaultBinaryMessenger.<fn>
package:flutter/src/services/binary_messenger.dart 86:4 defaultBinaryMessenger
package:flutter/src/services/platform_channel.dart 140:62 MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 393:5 MethodChannel.setMockMethodCallHandler
my_plugin_test.dart 9:13 main.<fn>
ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
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.
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.
package:flutter/src/services/binary_messenger.dart 73:7 defaultBinaryMessenger.<fn>
package:flutter/src/services/binary_messenger.dart 86:4 defaultBinaryMessenger
package:flutter/src/services/platform_channel.dart 140:62 MethodChannel.binaryMessenger
package:flutter/src/services/platform_channel.dart 393:5 MethodChannel.setMockMethodCallHandler
my_plugin_test.dart 15:13 main.<fn>
00:06 +0 -1: Some tests failed.
We should fix this failure, and also we should add a test to the Flutter CI to prevent this type of failure from occurring in the future.