The method 'setMockMessageHandler' isn't defined for the class 'BasicMessageChannel<dynamic>'

23.3k views Asked by At

After running the Flutter project, I get this error. What can I do to solve it?

Error: The method 'setMockMessageHandler' 
isn't defined for the class 'BasicMessageChannel<dynamic>'.

FAILURE: Build failed with an exception.
6

There are 6 answers

4
Jury Dpling On BEST ANSWER

I had the same problem after update plugin's in AndroidStudio on Mac

flutter pub upgrade

did nothing for me, but

flutter clean
flutter pub upgrade --major-versions

has solved the problem

2
Hosam Eisa On

I have the same problem, I have tried all the above and did not help. flutter pub upgrade and flutter pub upgrade --major-versions outputs this:

No dependencies changed.

1 package is discontinued.

76 packages have newer versions incompatible with dependency constraints.

Try flutter pub outdated for more information.

No changes to pubspec.yaml! The plugins advance_pdf_viewer, flutter_absolute_path, geocoder, google_api_headers, onesignal_flutter use a deprecated version of the Android embedding. To avoid unexpected runtime failures, or future build failures, try to see if these plugins support the Android V2 embedding. Otherwise, consider removing them since a future release of Flutter will remove these deprecated APIs. If you are plugin author, take a look at the docs for migrating the plugin to the V2 embedding: https://flutter.dev/go/android-plugin-migration.

0
thiago_silva On

I had the same problem today.

from what i could notice, this basically was a breaking change caused by transition of platform channel test interfaces to flutter_test package.

in my case, the problem was resolved just running the flutter pub upgrade on a global terminal session.

see more details about the mentioned transition on referred release notes

0
Zeus On

enter image description here

1

  1. : flutter pub upgrade
  2. : flutter clean
  3. : flutter pub upgrade --major-versions
  4. : cd ios
  5. : pod repo update
  6. : pod update
  7. : open Runner.xcworkspace
  8. : make clean & make run
1
Em Ikram On
  1. Go to specified class 'BasicMessageChannel' by press and hold ctrl and click on it
  2. than search for the 'setMockMessageHandler' by ctrl+F than u will see something like this // Looking for setMockMessageHandler? // See this shim package: packages/flutter_test/lib/src/deprecated.dart and paste this line below this comment void setMockMessageHandler(dynamic message){

} but this is just simple hack which is not recommended

0
Ber On

Running across this problem after upgrading to Flutter 2.5.3 (from 2.2.3). The change outlined in the release notes broke several hundred tests since the mock method handlers were set in the global setUp() for most tests.

As shown in the release notes, I replaced code like

 MethodChannel('channelName')
   .setMockMethodCallHandler((MethodCall methodCall) {});

with code using the default instance of TestDefaultBinaryMessenger:

 TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
   .setMockMethodCallHandler(MethodChannel('channelName'), (MethodCall methodCall) {});

This change allowed me to keep all tests unchanged.