@@ -416,6 +416,9 @@ class HardwareKeyboard {
416
416
final Map <PhysicalKeyboardKey , LogicalKeyboardKey > _pressedKeys =
417
417
< PhysicalKeyboardKey , LogicalKeyboardKey > {};
418
418
419
+ // Indicates whether the keyboard state has been initialized.
420
+ bool _keyboardStateInitialized = false ;
421
+
419
422
/// The set of [PhysicalKeyboardKey] s that are pressed.
420
423
///
421
424
/// If called from a key event handler, the result will already include the effect
@@ -505,6 +508,10 @@ class HardwareKeyboard {
505
508
}
506
509
507
510
void _assertEventIsRegular (KeyEvent event) {
511
+ if (! _keyboardStateInitialized) {
512
+ // Skip assertion if keyboard state is not initialized.
513
+ return ;
514
+ }
508
515
assert (() {
509
516
const String common =
510
517
'If this occurs in real application, please report this '
@@ -514,18 +521,18 @@ class HardwareKeyboard {
514
521
if (event is KeyDownEvent ) {
515
522
assert (
516
523
! _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 '
518
525
'key is already pressed. $common $event ' ,
519
526
);
520
527
} else if (event is KeyRepeatEvent || event is KeyUpEvent ) {
521
528
assert (
522
529
_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 '
524
531
'key is not pressed. $common $event ' ,
525
532
);
526
533
assert (
527
534
_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 '
529
536
'key is pressed on a different logical key. $common $event '
530
537
'and the recorded logical key ${_pressedKeys [event .physicalKey ]}' ,
531
538
);
@@ -601,6 +608,7 @@ class HardwareKeyboard {
601
608
_pressedKeys[physicalKey] = logicalKey;
602
609
}
603
610
}
611
+ _keyboardStateInitialized = true ;
604
612
}
605
613
606
614
bool _dispatchKeyEvent (KeyEvent event) {
0 commit comments