【发布时间】:2021-11-23 05:16:52
【问题描述】:
我正在开发一个需要对我们的服务器进行 api 调用的应用程序。我必须将 List 作为参数传递,但是当我尝试传递 List 时,它会像“[“Some Info 1”,“Some Info 2”]”这样的字符串。我尝试调试,当它执行 http 请求时,它会自动添加“”环绕列表。
这是我的服务类
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;
},
);
}
【问题讨论】:
-
编码你的参数,如
jsonEncode(_params)