From 78885ecbebf93a0c3667d1c82cdcc96b4963ab3c Mon Sep 17 00:00:00 2001 From: Jami Couch Date: Thu, 12 May 2022 17:59:13 -0500 Subject: [PATCH] Provide default method call handler for SystemChannels.textInput (#101087) --- packages/flutter/lib/src/services/binding.dart | 2 ++ packages/flutter/lib/src/services/text_input.dart | 6 ++++++ packages/flutter/test/services/binding_test.dart | 7 +++++++ 3 files changed, 15 insertions(+) diff --git a/packages/flutter/lib/src/services/binding.dart b/packages/flutter/lib/src/services/binding.dart index bb02fc9b104c..5b62be9e0cdd 100644 --- a/packages/flutter/lib/src/services/binding.dart +++ b/packages/flutter/lib/src/services/binding.dart @@ -17,6 +17,7 @@ import 'message_codec.dart'; import 'raw_keyboard.dart'; import 'restoration.dart'; import 'system_channels.dart'; +import 'text_input.dart'; /// Listens for platform messages and directs them to the [defaultBinaryMessenger]. /// @@ -36,6 +37,7 @@ mixin ServicesBinding on BindingBase, SchedulerBinding { SystemChannels.system.setMessageHandler((dynamic message) => handleSystemMessage(message as Object)); SystemChannels.lifecycle.setMessageHandler(_handleLifecycleMessage); SystemChannels.platform.setMethodCallHandler(_handlePlatformMessage); + TextInput.ensureInitialized(); readInitialLifecycleStateFromNativeWindow(); } diff --git a/packages/flutter/lib/src/services/text_input.dart b/packages/flutter/lib/src/services/text_input.dart index 1b43953f12dd..fecf86f7e92d 100644 --- a/packages/flutter/lib/src/services/text_input.dart +++ b/packages/flutter/lib/src/services/text_input.dart @@ -1575,6 +1575,12 @@ class TextInput { TextInputAction.emergencyCall, ]; + /// Ensure that a [TextInput] instance has been set up so that the platform + /// can handle messages on the text input method channel. + static void ensureInitialized() { + _instance; // ignore: unnecessary_statements + } + /// Begin interacting with the text input control. /// /// Calling this function helps multiple clients coordinate about which one is diff --git a/packages/flutter/test/services/binding_test.dart b/packages/flutter/test/services/binding_test.dart index fd8140a3e0c6..fad46065b919 100644 --- a/packages/flutter/test/services/binding_test.dart +++ b/packages/flutter/test/services/binding_test.dart @@ -107,4 +107,11 @@ void main() { await rootBundle.loadString('test_asset2'); expect(flutterAssetsCallCount, 4); }); + + test('initInstances sets a default method call handler for SystemChannels.textInput', () async { + final ByteData message = const JSONMessageCodec().encodeMessage({'method': 'TextInput.requestElementsInRect', 'args': null})!; + await ServicesBinding.instance.defaultBinaryMessenger.handlePlatformMessage('flutter/textinput', message, (ByteData? data) { + expect(data, isNotNull); + }); + }); }