I'm trying to find a best way to reuse http headers in my http responses. Instead of writing it in string literal
final http.Response response = await http.post(APIPath.somePath(),
headers:{"Content-Type": "application/json","Authorization": "Bearer $_token"},
body: json.encode(body));
I have made a custom class and get each header into a static function
class APIHeader {
static Map<String, String> json() => {"Content-Type": "application/json"};
static Map<String, String> form() => {"Content-Type": "multipart/form-data"};
static Map<String, String> authorization(String token) =>
{"Authorization": "Bearer $token"};
}
and call them wherever I need them which work great if there is only one header needed
final http.Response response = await http.put(APIPath.somePath(),
headers: APIHeader.json(), body: json.encode(body));
However I'm having a trouble if I need more then one header. I tried this..
final header = {}
..addAll(APIHeader.authorization(_token))
..addAll(APIHeader.json());
final http.Response response = await http.post(APIPath.somePath(),
headers: header, body: json.encode(body));
which gives me an error
Unhandled Exception: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>'
Anyone have better idea how to reuse the headers?
final header = {}->final header = <String, String>{}mergeMapsfor example (just found it one minute ago so i dont know how it works in practice)import 'package:collection/collection.dart'; var maps = [{1: 10}, {2: 20}, {3: 30}]; print(maps); print(maps.reduce(mergeMaps));