opt<T> function

T? opt<T>(
  1. Map<String, dynamic> json,
  2. String key
)

Reads an optional key of type T. A wrong type reads as absent — the server is allowed to widen a field without breaking older clients.

dart:convert decodes a JSON whole number (e.g. 2) as int, never double. Read numeric fields as num or intT = double will silently read a valid whole-number value as absent.

Implementation

T? opt<T>(Map<String, dynamic> json, String key) {
  final value = json[key];
  return value is T ? value : null;
}