# findKeyType<K> method

1. @override

Class<K>? findKeyType<K>(

1. Class parentClass,
2. Type? key

)
override

Finds the key type of a map-like class.

Type Parameters:

- `K`: The expected key type.

Parameters:

- `parentClass`: The class representing the map.
- `key`: The runtime Type to help narrow the match (optional).

Returns:

- A Class<K> representing the key type, or `null` if none exists.

## Example

    final mapClass = await loader.loadClass<Map<String, int>>('dart:core/map.dart.Map');
    final stringType = loader.findKeyType<String>(mapClass);
    print(stringType?.qualifiedName); // "dart:core/string.dart.String"

## Implementation

    @override
    Class<K>? findKeyType<K>(Class parentClass, Type? key) {
      _checkClosed();

      final cacheKey = "${_buildCacheKey(parentClass)}:${key ?? ""}:classKey";

      // Check cache first
      final cached = _classKeyCache[cacheKey];
      if (cached != null) {
        _hitCount++;
        return cached as Class<K>;
      }

      _missCount++;

      key ??= extractKeyType(parentClass);

      Class<K>? result;
      if (key == null) {
        result = _extractKeyTypeFromName<K>(parentClass);
      } else if(key.toString().contains("dynamic") && !parentClass.getName().contains("dynamic")) {
        result = _extractKeyTypeFromName<K>(parentClass);
      } else {
        result = Class.forType<K>(key as K, parentClass.getProtectionDomain());
      }

      // Cache result
      if(result != null) {
        _classKeyCache[cacheKey] = result;
      }

      return result;
    }