Repository: flutter/flutter
File: packages/flutter/lib/src/services/hardware_keyboard.dart (Dart)

Code before:
        return true;
      }());
      return shouldProcess;
    } else if (event is KeyRepeatEvent || event is KeyUpEvent) {
      // Only checks physical key here and don't reject based on logical key,
      // since otherwise the key will never be released.
      final bool shouldProcess = _pressedKeys.containsKey(event.physicalKey);
      assert(() {
        if (!shouldProcess) {
          _keyboardDebug(
            () =>
                'Received ${event.runtimeType} for key that is not pressed:\n'
                'Event: $event',
          );
        } else if (_pressedKeys[event.physicalKey] != event.logicalKey) {
          _keyboardDebug(
            () =>
                'Received ${event.runtimeType} for key with mismatched logical key:\n'
                'Event: $event\n'
                'Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
        return true;
      }());
      return shouldProcess;
    } else {
      assert(() {
        _keyboardDebug(
          () =>
              'Received unknown KeyEvent subtype:\n'
              'Event: $event',
        );
        return true;
      }());
      return false;
    }
  }

  List<KeyEventCallback> _handlers = <KeyEventCallback>[];
  bool _duringDispatch = false;
  List<KeyEventCallback>? _modifiedHandlers;

  /// Register a listener that is called every time a hardware key event
  /// occurs.
  ///
  /// All registered handlers will be invoked in order regardless of
  /// their return value. The return value indicates whether Flutter
  /// "handles" the event. If any handler returns true, the event
  /// will not be propagated to other native components in the add-to-app
  /// scenario.
  ///

Review comment (suggestion):
This branch is still unexpected, no? Should we keep the assertion?

My understanding is if this is reachable, it indicates that we introduced a new `KeyEvent` but forgot to update this code path.

Code after:
            () =>
                'ERROR: Received unexpected ${event.runtimeType} for key that is not pressed:\n'
                '$common\n'
                '    Event: $event',
          );
        } else if (_pressedKeys[event.physicalKey] != event.logicalKey) {
          _keyboardDebug(
            () =>
                'ERROR: Received unexpected ${event.runtimeType} for key with mismatched logical key:\n'
                '$common\n'
                '    Event: $event\n'
                '    Pressed logical key: ${_pressedKeys[event.physicalKey]}',
          );
        }
      } else {
        assert(false, 'Received unexpected key event class ${event.runtimeType}');
      }
      return true;
    }());
  }

  List<KeyEventCallback> _handlers = <KeyEventCallback>[];
  bool _duringDispatch = false;
  List<KeyEventCallback>? _modifiedHandlers;

  /// Register a listener that is called every time a hardware key event
  /// occurs.
  ///
  /// All registered handlers will be invoked in order regardless of
  /// their return value. The return value indicates whether Flutter
  /// "handles" the event. If any handler returns true, the event
  /// will not be propagated to other native components in the add-to-app
  /// scenario.
  ///
  /// If an object added a handler, it must remove the handler before it is
  /// disposed.
  ///
  /// If used during event dispatching, the addition will not take effect
  /// until after the dispatching.
  ///
  /// See also:
  ///
  ///  * [removeHandler], which removes the handler.
  void addHandler(KeyEventCallback handler) {
    if (_duringDispatch) {
      _modifiedHandlers ??= <KeyEventCallback>[..._handlers];
      _modifiedHandlers!.add(handler);
    } else {
      _handlers.add(handler);
    }
  }