post<T> method

Future<T> post<T>(
  1. String path, {
  2. dynamic body,
  3. Map<String, String>? headers,
  4. Map<String, dynamic>? queryParameters,
})

Sends an HTTP POST request to path with optional body and decodes the JSON response as T.

If body is not a String, it is automatically serialized to JSON via jsonEncode.

Throws an http.ClientException if the server returns a non-2xx status code.

final created = await client.post<Map<String, dynamic>>(
  '/tasks',
  body: {'title': 'Write docs', 'completed': false},
);

Implementation

Future<T> post<T>(
  String path, {
  dynamic body,
  Map<String, String>? headers,
  Map<String, dynamic>? queryParameters,
}) async {
  final res = await _send('POST', path,
      body: body, headers: headers, queryParameters: queryParameters);
  return res as T;
}