diff --git a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart index 3c10d09a481..4567f414fb6 100644 --- a/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart +++ b/engine/src/flutter/lib/web_ui/lib/src/engine/keyboard_binding.dart @@ -586,6 +586,11 @@ class KeyboardConverter { // * Some key data might be synthesized to update states after the main key // data. They are always scheduled asynchronously with results discarded. void handleEvent(FlutterHtmlKeyboardEvent event) { + // Autofill on Chrome sends keyboard events whose key and code are null. + if (event.key == null || event.code == null) { + return; + } + assert(_dispatchKeyData == null); bool sentAnyEvents = false; _dispatchKeyData = (ui.KeyData data) { diff --git a/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart b/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart index d381393fa89..dab4914f64f 100644 --- a/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart +++ b/engine/src/flutter/lib/web_ui/test/engine/keyboard_converter_test.dart @@ -1142,6 +1142,21 @@ void testMain() { ); keyDataList.clear(); }); + + test('Ignore DOM event when event.key is null', () { + // Regression test for https://github.com/flutter/flutter/issues/114620. + final List keyDataList = []; + final KeyboardConverter converter = KeyboardConverter((ui.KeyData key) { + keyDataList.add(key); + return true; + }, OperatingSystem.linux); + + converter.handleEvent(keyDownEvent(null, null)); + converter.handleEvent(keyUpEvent(null, null)); + + // Invalid key events are ignored. + expect(keyDataList, isEmpty); + }); } // Flags used for the `modifiers` argument of `key***Event` functions. @@ -1153,7 +1168,7 @@ const int kMeta = 0x8; // Utility functions to make code more concise. // // To add timeStamp , use syntax `..timeStamp = `. -MockKeyboardEvent keyDownEvent(String code, String key, [int modifiers = 0, int location = 0]) { +MockKeyboardEvent keyDownEvent(String? code, String? key, [int modifiers = 0, int location = 0]) { return MockKeyboardEvent( type: 'keydown', code: code, @@ -1166,7 +1181,7 @@ MockKeyboardEvent keyDownEvent(String code, String key, [int modifiers = 0, int ); } -MockKeyboardEvent keyUpEvent(String code, String key, [int modifiers = 0, int location = 0]) { +MockKeyboardEvent keyUpEvent(String? code, String? key, [int modifiers = 0, int location = 0]) { return MockKeyboardEvent( type: 'keyup', code: code,