get<T> method

T get<T>(
  1. String key,
  2. T defaultValue
)

Typed lookup. The generic T carries the expected type so the caller gets the right type back without casting. If the stored value doesn't match T (e.g. the server shipped a string where the caller asked for int), defaultValue is returned — a mismatch is the same as a missing key.

Supported Ts: String, int, double, bool, Map, List, and num (accepts both int and float).

Implementation

T get<T>(String key, T defaultValue) {
  final decision = _resolve(key);
  if (decision == null || decision.value == null) return defaultValue;
  final v = decision.value;
  if (v is T) return v;
  // Allow int → double and double → int narrowing because the server
  // emits both as `num` over JSON; Dart clients may ask for either.
  if (T == double && v is num) return v.toDouble() as T;
  if (T == int && v is num) return v.toInt() as T;
  // Bool coercion — a dashboard item declared as a number (1/0) or string
  // ("true"/"false"/"1"/"0") read as bool by the client used to silently
  // return the default. Accept the common cross-type encodings.
  if (T == bool) {
    if (v is num) return (v != 0) as T;
    if (v is String) {
      final s = v.trim().toLowerCase();
      if (s == 'true' || s == '1') return true as T;
      if (s == 'false' || s == '0') return false as T;
    }
    return defaultValue;
  }
  return defaultValue;
}