fromJson static method

InputKey fromJson(
  1. Map<String, Object?> json
)

Rebuilds a key from toJson's output.

This is the one place a switch over the sealed hierarchy is needed, and it is not the "registry" the design rules out: it is closed (sealed means the analyzer fails this switch when a device is added, rather than a lookup silently missing an entry) and it resolves nothing the caller could have told us instead - a JSON map genuinely does not know its own Dart type.

Implementation

static InputKey fromJson(Map<String, Object?> json) {
  final kind = json['kind'];
  switch (kind) {
    case KeyboardKey.kindName:
      return KeyboardKey.fromJson(json);
    case MouseButtonKey.kindName:
      return MouseButtonKey.fromJson(json);
    case GamepadKey.kindName:
      return GamepadKey.fromJson(json);
    default:
      throw FormatException(
        'unknown InputKey kind "$kind" - expected '
        '"${KeyboardKey.kindName}", "${MouseButtonKey.kindName}" or '
        '"${GamepadKey.kindName}"',
        json,
      );
  }
}