readOne<T> method

Future<T> readOne<T>(
  1. T fromMap(
    1. Map<String, dynamic>
    )
)

Reads the response body as JSON and applies the fromMap function to create an object.

Example:

final data = await client.get('https://example.com').readOne((json) => MyObject.fromMap(json));

Implementation

Future<T> readOne<T>(T Function(Map<String, dynamic>) fromMap) async {
  final response = await go();

  if (response.statusCode == HttpStatus.ok) {
    final jsonData = await response.transform(utf8.decoder).join();
    return fromMap(jsonDecode(jsonData));
  } else {
    throw Exception('Request failed with status: ${response.statusCode}');
  }
}