Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter http headers

Tags:

The post request is throwing an error while setting the header map.

Here is my code

Future<GenericResponse> makePostCall(   GenericRequest genericRequest) {String URL = "$BASE_URL/api/";  Map data = {   "name": "name",   "email": "email",   "mobile": "mobile",   "transportationRequired": false,   "userId": 5, };  Map userHeader = {"Content-type": "application/json", "Accept": "application/json"};   return _netUtil.post(URL, body: data, headers:userHeader).then((dynamic res) {   print(res);   if (res["code"] != 200) throw new Exception(res["message"][0]);   return GenericResponse.fromJson(res); });  } 

but I'm getting this exception with headers.

══╡ EXCEPTION CAUGHT BY GESTURE ╞═ flutter: The following assertion was thrown while handling a gesture: flutter: type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, String>' flutter: flutter: Either the assertion indicates an error in the framework itself, or we should provide substantially flutter: more information in this error message to help you determine and fix the underlying cause. flutter: In either case, please report this assertion by filing a bug on GitHub: flutter:   https://github.com/flutter/flutter/issues/new?template=BUG.md flutter: flutter: When the exception was thrown, this was the stack: flutter: #0      NetworkUtil.post1 (package:saranam/network/network_util.dart:50:41) flutter: #1      RestDatasource.bookPandit (package:saranam/network/rest_data_source.dart:204:21) 

Anybody facing this issue? I didn't find any clue with the above log.

like image 334
Satya Attili Avatar asked Nov 14 '18 11:11

Satya Attili


People also ask

What is header in flutter?

Flutter Sticky HeadersLets you place headers on scrollable content that will stick to the top of the container whilst the content is scrolled.


2 Answers

Try

 Map<String, String> requestHeaders = {        'Content-type': 'application/json',        'Accept': 'application/json',        'Authorization': '<Your token>'      }; 
like image 55
Sami Kanafani Avatar answered Sep 19 '22 06:09

Sami Kanafani


You can try this:

Map<String, String> get headers => {         "Content-Type": "application/json",         "Accept": "application/json",         "Authorization": "Bearer $_token",       }; 

and then along with your http request for header just pass header as header

example:

Future<AvatarResponse> getAvatar() async {     var url = "$urlPrefix/api/v1/a/me/avatar";     print("fetching $url");     var response = await http.get(url, headers: headers);     if (response.statusCode != 200) {       throw Exception(           "Request to $url failed with status ${response.statusCode}: ${response.body}");     }      var avatar = AvatarResponse()       ..mergeFromProto3Json(json.decode(response.body),           ignoreUnknownFields: true);     print(avatar);     return avatar;   } 
like image 45
Javad Moradi Avatar answered Sep 20 '22 06:09

Javad Moradi