diff --git a/packages/flutter/lib/src/services/platform_messages.dart b/packages/flutter/lib/src/services/platform_messages.dart index b5b3a22db6..b1f7f8836b 100644 --- a/packages/flutter/lib/src/services/platform_messages.dart +++ b/packages/flutter/lib/src/services/platform_messages.dart @@ -37,7 +37,13 @@ class BinaryMessages { static Future _sendPlatformMessage(String channel, ByteData message) { final Completer completer = Completer(); - ServicesBinding.instance.window.sendPlatformMessage(channel, message, (ByteData reply) { + // ui.window is accessed directly instead of using ServicesBinding.instance.window + // because this method might be invoked before any binding is initialized. + // This issue was reported in #27541. It is not ideal to statically access + // ui.window because the Window may be dependency injected elsewhere with + // a different instance. However, static access at this location seems to be + // the least bad option. + ui.window.sendPlatformMessage(channel, message, (ByteData reply) { try { completer.complete(reply); } catch (exception, stack) {