getRestPayload method

Future<Map<String, dynamic>> getRestPayload()

Fetch from the rest endpoint

Implementation

Future<Map<String, dynamic>> getRestPayload() async {
  final resp = await client.get(Uri.parse(endpoint), headers: headers);
  if (resp.statusCode != 200) {
    throw StateError('Request unsuccessful; status code ${resp.statusCode}');
  }

  final result = jsonDecode(resp.body);
  if (result is List) {
    if (result.first is List) {
      throw StateError("Can't process nested, top-level arrays");
    }
    return result.first;
  } else {
    if (topLevelKey != null) {
      final res = result[topLevelKey];
      if (res is List) {
        return res.first;
      }

      return res;
    }

    return result;
  }
}