mergeHeaders static method

Map<String, String> mergeHeaders(
  1. Map<String, String> first, [
  2. Map<String, String>? second,
  3. Map<String, String>? third,
  4. Map<String, String>? fourth,
])

Merges multiple header maps into one.

Example:

final res = await client.post(
  '/api/users',
  headers: RequestHelper.mergeHeaders(
    RequestHelper.jsonHeaders,
    AuthHelper.bearer(token),
    {'x-custom': 'value'},
  ),
  body: jsonEncode({'name': 'John'}),
);

Implementation

static Map<String, String> mergeHeaders(
  Map<String, String> first, [
  Map<String, String>? second,
  Map<String, String>? third,
  Map<String, String>? fourth,
]) {
  return {
    ...first,
    if (second != null) ...second,
    if (third != null) ...third,
    if (fourth != null) ...fourth,
  };
}