get<T> method

T get<T>(
  1. String key, {
  2. ConfigScope? scope,
  3. T? defaultValue,
})

Retrieve a typed value for key.

Resolution order: session > project > global > defaultValue > built-in default. If scope is given, only that scope is searched.

Implementation

T get<T>(String key, {ConfigScope? scope, T? defaultValue}) {
  if (scope != null) {
    final map = _store[scope]!;
    if (map.containsKey(key)) return map[key] as T;
    if (defaultValue != null) return defaultValue;
    if (_defaults.containsKey(key)) return _defaults[key] as T;
    throw StateError('Config key "$key" not found in scope $scope');
  }

  // Walk scopes from narrowest to broadest.
  for (final s in [
    ConfigScope.session,
    ConfigScope.project,
    ConfigScope.global,
  ]) {
    final map = _store[s]!;
    if (map.containsKey(key)) return map[key] as T;
  }
  if (defaultValue != null) return defaultValue;
  if (_defaults.containsKey(key)) return _defaults[key] as T;
  throw StateError('Config key "$key" not found');
}