Skip to content

Commit beee39e

Browse files
committed
apply change on top main-stream branch
1 parent b152ced commit beee39e

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

packages/flutter/lib/src/services/hardware_keyboard.dart

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,9 @@ class HardwareKeyboard {
416416
final Map<PhysicalKeyboardKey, LogicalKeyboardKey> _pressedKeys =
417417
<PhysicalKeyboardKey, LogicalKeyboardKey>{};
418418

419+
// Indicates whether the keyboard state has been initialized.
420+
bool _keyboardStateInitialized = false;
421+
419422
/// The set of [PhysicalKeyboardKey]s that are pressed.
420423
///
421424
/// If called from a key event handler, the result will already include the effect
@@ -505,6 +508,10 @@ class HardwareKeyboard {
505508
}
506509

507510
void _assertEventIsRegular(KeyEvent event) {
511+
if (!_keyboardStateInitialized) {
512+
// Skip assertion if keyboard state is not initialized.
513+
return;
514+
}
508515
assert(() {
509516
const String common =
510517
'If this occurs in real application, please report this '
@@ -514,18 +521,18 @@ class HardwareKeyboard {
514521
if (event is KeyDownEvent) {
515522
assert(
516523
!_pressedKeys.containsKey(event.physicalKey),
517-
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
524+
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
518525
'key is already pressed. $common$event',
519526
);
520527
} else if (event is KeyRepeatEvent || event is KeyUpEvent) {
521528
assert(
522529
_pressedKeys.containsKey(event.physicalKey),
523-
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
530+
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
524531
'key is not pressed. $common$event',
525532
);
526533
assert(
527534
_pressedKeys[event.physicalKey] == event.logicalKey,
528-
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
535+
'A ${event.runtimeType} is dispatched, but the state shows that the physical '
529536
'key is pressed on a different logical key. $common$event '
530537
'and the recorded logical key ${_pressedKeys[event.physicalKey]}',
531538
);
@@ -601,6 +608,7 @@ class HardwareKeyboard {
601608
_pressedKeys[physicalKey] = logicalKey;
602609
}
603610
}
611+
_keyboardStateInitialized = true;
604612
}
605613

606614
bool _dispatchKeyEvent(KeyEvent event) {

0 commit comments

Comments
 (0)