getting PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle"., null, null) error when running test cases
I am using wakelock_plus for one of my classes to keep player screen on. Test cases related to that file are failing. I was earlier using wakelock, recently updated to wakelock_plus. In the test case file I have a setupAll method where earlier for wakelock 'wakelock_macos' was used for MethodChannel name like below.
const channel = MethodChannel('wakelock_macos');
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
return null;
});
for this test cases were passed successfully.
After comparing the wakelock and wakelock_plus libraries, I attempted to utilise the MethodChannel named 'wakelock_plus_macos' for wakelock_plus in the same way that we used it for wakelock.
const channel = MethodChannel('wakelock_plus_macos');
But test cases are failing with below error:
PlatformException(channel-error, Unable to establish connection on channel: "dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle"., null, null)
Now if I use 'dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle' as the MethodChannel name like
const channel = MethodChannel('dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle');
I am getting another error:
FormatException: Message corrupted
package:flutter/src/services/message_codecs.dart 532:16 StandardMessageCodec.readValueOfType
package:flutter/src/services/message_codecs.dart 477:12 StandardMessageCodec.readValue
package:flutter/src/services/message_codecs.dart 522:23 StandardMessageCodec.readValueOfType
package:flutter/src/services/message_codecs.dart 477:12 StandardMessageCodec.readValue
package:flutter/src/services/message_codecs.dart 609:41 StandardMethodCodec.decodeMethodCall
package:flutter_test/src/test_default_binary_messenger.dart 300:45 TestDefaultBinaryMessenger.setMockMethodCallHandler.<fn>
package:flutter_test/src/test_default_binary_messenger.dart 143:29 TestDefaultBinaryMessenger.send
package:flutter/src/services/platform_channel.dart 218:54 BasicMessageChannel.send
package:wakelock_plus_platform_interface/messages.g.dart 122:32 WakelockPlusApi.toggle
package:wakelock_plus_platform_interface/src/method_channel_wakelock_plus.dart 20:16 MethodChannelWakelockPlus.toggle
package:wakelock_plus/wakelock_plus.dart 71:41 WakelockPlus.toggle
What should be used for MethodChannel to run test cases successfully
For those who are facing such issue, I was able to pass the test cases by making below change in my test file.
I have replaced
const channel = MethodChannel('wakelock_macos');
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMethodCallHandler(channel, (MethodCall methodCall) async {
return null;
});
to
TestDefaultBinaryMessengerBinding.instance.defaultBinaryMessenger
.setMockMessageHandler(
'dev.flutter.pigeon.wakelock_plus_platform_interface.WakelockPlusApi.toggle',
(obj) async => obj);
I face this error also when using the plugin in production. I'm using wakelock_plus: 1.1.4
@luckyhandler @shivamtiwari-ttn is there any fix for it ?
@diegotori Most likely related to Flutter upgrade (3.24.X)
@luckyhandler Have you upgraded Flutter version recently?
@shivamtiwari-ttn please upgrade your wakelock_plus version to the latest 1.2.x version.
The latest ones are guaranteed to be compatible with Flutter 3.19 and higher.
When upgrading, also run flutter pub upgrade so that it pulls in the latest transitive dependencies that this library uses.
Thanks @diegotori, we will try to upgrade versions.
So, it be caused because of mismatch in versions between platform interface and the package itself, right?
pubspec.lock
wakelock_plus:
dependency: "direct main"
description:
name: wakelock_plus
sha256: f268ca2116db22e57577fb99d52515a24bdc1d570f12ac18bb762361d43b043d
url: "https://pub.dev"
source: hosted
version: "1.1.4"
wakelock_plus_platform_interface:
dependency: transitive
description:
name: wakelock_plus_platform_interface
sha256: "422d1cdbb448079a8a62a5a770b69baa489f8f7ca21aef47800c726d404f9d16"
url: "https://pub.dev"
source: hosted
version: "1.2.1"
Thanks @diegotori, we will try to upgrade versions.
So, it be caused because of mismatch in versions between platform interface and the package itself, right?
pubspec.lockwakelock_plus: dependency: "direct main" description: name: wakelock_plus sha256: f268ca2116db22e57577fb99d52515a24bdc1d570f12ac18bb762361d43b043d url: "https://pub.dev" source: hosted version: "1.1.4" wakelock_plus_platform_interface: dependency: transitive description: name: wakelock_plus_platform_interface sha256: "422d1cdbb448079a8a62a5a770b69baa489f8f7ca21aef47800c726d404f9d16" url: "https://pub.dev" source: hosted version: "1.2.1"
Once you upgrade both, it should work itself out.
crash info:
E/flutter (13571): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: FormatException: Message corrupted
E/flutter (13571): #0 StandardMessageCodec.readValueOfType (package:flutter/src/services/message_codecs.dart:532:16)
E/flutter (13571): #1 _WakelockPlusApiCodec.readValueOfType (package:wakelock_plus_platform_interface/messages.g.dart:96:22)
E/flutter (13571): #2 StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:477:12)
E/flutter (13571): #3 StandardMessageCodec.readValueOfType (package:flutter/src/services/message_codecs.dart:522:23)
E/flutter (13571): #4 _WakelockPlusApiCodec.readValueOfType (package:wakelock_plus_platform_interface/messages.g.dart:96:22)
E/flutter (13571): #5 StandardMessageCodec.readValue (package:flutter/src/services/message_codecs.dart:477:12)
E/flutter (13571): #6 StandardMessageCodec.decodeMessage (package:flutter/src/services/message_codecs.dart:341:28)
E/flutter (13571): #7 BasicMessageChannel.send (package:flutter/src/services/platform_channel.dart:218:18)
E/flutter (13571): <asynchronous suspension>
E/flutter (13571): #8 WakelockPlusApi.isEnabled (package:wakelock_plus_platform_interface/messages.g.dart:146:9)
E/flutter (13571): <asynchronous suspension>
E/flutter (13571): #9 MethodChannelWakelockPlus.enabled (package:wakelock_plus_platform_interface/src/method_channel_wakelock_plus.dart:10:21)
E/flutter (13571): <asynchronous suspension>
version:
wakelock_plus: dependency: transitive description: name: wakelock_plus sha256: "1aeab49f24aec1e5ab417d7cdfc47c7bbcb815353f1840667ffe68c89a0cd2e6" url: "https://pub.dev" source: hosted version: "1.2.9" wakelock_plus_platform_interface: dependency: transitive description: name: wakelock_plus_platform_interface sha256: "422d1cdbb448079a8a62a5a770b69baa489f8f7ca21aef47800c726d404f9d16" url: "https://pub.dev" source: hosted version: "1.2.1"
flutter doctor:
Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 3.24.2, on macOS 14.0 23A344 darwin-arm64, locale en-CN) [✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0) [✓] Xcode - develop for iOS and macOS (Xcode 15.4) [✓] Chrome - develop for the web [✓] Android Studio (version 2024.1) [✓] VS Code (version 1.96.0) [✓] Connected device (5 available) ! Error: Browsing on the local area network for iPhone (2). Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) ! Error: Browsing on the local area network for Adam 的iPhone. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) ! Error: Browsing on the local area network for iPhone (3). Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) ! Error: Browsing on the local area network for ejunyue. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) ! Error: Browsing on the local area network for iPhone14pm. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac. The device must be opted into Developer Mode to connect wirelessly. (code -27) [✓] Network resources
• No issues found!
@diegotori
Please upgrade your versions, since I just deployed new versions recently. Version 1.2.10 for wakelock_plus and version 1.2.2 for wakelock_plus_platform_interface, which now use the latest version of Pigeon.
Wow, you are so efficient. problem solved, thanks a lot.