Currently I'm sending header with every request like as follow which is very repetitive. Is there any process so that all my request will have a request header automatically ? Or how can I avoid code repetition for the following lines:
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString('accessToken');
headers: {
'Contet-type': 'application/json',
'Authorization': 'Bearer $token',
}
My complete API Request code:
Future<http.Response> getAUser(userId) async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
String token = sharedPreferences.getString('accessToken');
return await http.get(
'$baseUrl/user/$userId/',
headers: {
'Contet-type': 'application/json',
'Authorization': 'Bearer $token',
},
).timeout(Duration(seconds: 30));
}