Skip to content

flutter create --template plugin has failing tests #39077

@collinjackson

Description

@collinjackson

Recently there was a breaking change:

https://groups.google.com/forum/#!msg/flutter-announce/sHAL2fBtJ1Y/mGjrKH3dEwAJ

#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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    a: tests"flutter test", flutter_test, or one of our testsc: regressionIt was better in the past than it is nowpackageflutter/packages repository. See also p: labels.waiting for PR to land (fixed)A fix is in flight

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions