I'm developing an app which required api call to our server. I have to pass List as Parameter but when I try to pass List it go as String like "["Some Info 1","Some Info 2"]". I have try to debug and and when it's doing http request it's automatically add "" surround list.
This my Service Class
Future<Map<String, dynamic>> post(
String _url, Map<String, String> _headers, Map<String, String> _params) {
print('_url => $_url');
if (_headers != null) {
print('_headers => $_headers');
}
print('_params => $_params');
return http
.post(Uri.parse(BASE_URL _url),
headers: (_headers != null) ? _headers : {}, body: _params)
.then(
(response) {
final code = response.statusCode;
final body = response.body;
final jsonBody = json.decode(body);
debugPrint('response code => $code');
debugPrint('response body => $body');
Map<String, dynamic> _resDic;
if (code == 200 || code == 300) {
_resDic = Map<String, dynamic>.from(jsonBody);
_resDic[HTTP_CODE] = code;
print('Success => ${_resDic[HTTP_CODE]}');
if (code == 422) {
SharedPref.saveString(AUTH_TOKEN, '');
SharedPref.saveInt(USER_TYPE, 0);
SharedPref.saveObject(USER, null);
SharedPref.resetData();
navigationKey.currentState
.pushNamedAndRemoveUntil(LOGIN, (route) => false);
if (_resDic[IS_TOKEN_EXPIRED] == 1) {
_resDic[HTTP_CODE] = 422;
}
}
} else {
_resDic = Map<String, dynamic>();
_resDic[HTTP_CODE] = code;
_resDic[IS_TOKEN_EXPIRED] = 0;
_resDic[MESSAGE] = jsonBody[MESSAGE] != null
? jsonBody[MESSAGE]
: 'Something went wrong';
_resDic[HTTP_CODE] = code;
}
print('===>> Response : $_resDic');
return _resDic;
},
);
}
CodePudding user response:
Encode your params with
json.encode(params)
CodePudding user response:
I solved it using this way.
generateHeaders() async {
String _token = await SharedPref.readString(AUTH_TOKEN);
return {
'Authorization': 'Bearer $_token',
"Accept": "application/json",
"Content-Type": "application/json"
};
}
Future<Map<String, dynamic>> postDynamic(
String _url, Map<String, String> _headers, Map<String, dynamic> _params) {
print('_url => $_url');
if (_headers != null) {
print('_headers => $_headers');
}
print('_params => $_params');
return http
.post(Uri.parse(BASE_URL _url),
headers: (_headers != null) ? _headers : {},
body: json.encode(_params))
.then(
(response) {
final code = response.statusCode;
final body = response.body;
final jsonBody = json.decode(body);
debugPrint('response code => $code');
debugPrint('response body => $body');
print('response code => $code');
Map<String, dynamic> _resDic;
if (code == 200 || code == 300) {
_resDic = Map<String, dynamic>.from(jsonBody);
_resDic[HTTP_CODE] = code;
print('Success => ${_resDic[HTTP_CODE]}');
} else if (code == 401) {
SharedPref.saveString(AUTH_TOKEN, '');
SharedPref.saveInt(USER_TYPE, 0);
SharedPref.saveObject(USER, null);
SharedPref.resetData();
navigationKey.currentState
.pushNamedAndRemoveUntil(LOGIN, (route) => false);
} else {
_resDic = Map<String, dynamic>();
_resDic[HTTP_CODE] = code;
_resDic[IS_TOKEN_EXPIRED] = 0;
_resDic[MESSAGE] = jsonBody[MESSAGE] != null
? jsonBody[MESSAGE]
: 'Something went wrong';
_resDic[HTTP_CODE] = code;
}
print('===>> Response : $_resDic');
return _resDic;
},
);
}