fetchList<T> static method

Future<List<T>?> fetchList<T>(
  1. Uri uri
)

Fetches a JSON list from a URI. Returns null if the request fails or decoding fails.

Implementation

static Future<List<T>?> fetchList<T>(Uri uri) async {
  try {
    final response = await http.get(uri);
    if (response.statusCode != 200) return null;

    final body = response.body.trim();
    // Early rejection if response doesn't start with expected JSON array
    if (!body.startsWith('[')) return null;

    final decoded = json.decode(body);
    if (decoded is List && decoded.every((element) => element is T)) {
      return decoded.cast<T>();
    }
  } catch (_) {
    // Silent fail
  }
  return null;
}