[Breaking Change] Proposed change to move the default BinaryMessenger instance to ServicesBinding

2,771 views
Skip to first unread message

Ada Zhang

unread,
Aug 8, 2019, 12:20:00 PM8/8/19
to Flutter Public Announcements (flutter-announce)
Hi all,

https://github.com/flutter/flutter/pull/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 https://github.com/flutter/flutter/issues/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: https://github.com/flutter/flutter/issues/37409 -- or just reply to this email.

Thanks,
Ada

Ada Zhang

unread,
Aug 15, 2019, 12:42:09 PM8/15/19
to Flutter Public Announcements (flutter-announce)
An update to this post:

The original PR (https://github.com/flutter/flutter/pull/37489) has been reverted and we are going to re-land this change in https://github.com/flutter/flutter/pull/38464.

Additional migration info
By this change, ServicesBinding has to be initialized before accessing the default binary messenger (e.g. when using a platform channel/plugin). You'll see a `FlutterError` if the ServicesBinding is not 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.
Reply all
Reply to author
Forward
0 new messages