json - Flutter:当我尝试在 post requestFl 的正文中将 int 转换为 json 时出错
问题描述
当我尝试将 dart 代码转换为 json 以进行发布请求时,我收到此错误“类型'int'不是类型转换中类型'String'的子类型”',
在请求正文中要求 [numberOfPerson] 为整数。
这是我用来发布请求的方法:
Future<void> createReservation(String fromTime, String toTime, String areaId,
int numberOfPerson, String date) async {
Map<String, dynamic> data = Map<String, dynamic>();
data['area_id'] = areaId;
data['number_of_people'] = numberOfPerson;
data['from_time'] = date + " " + fromTime;
data['to_time'] = date + " " + toTime;
final prefs = await SharedPreferences.getInstance();
if (prefs.containsKey(Constant.prefsUserAccessTokenKey)) {
token = prefs.getString(Constant.prefsUserAccessTokenKey);
}
final url = Urls.create_reservasion;
try {
final response = await retry(
() => http
.post(url,
headers:
'Authorization': 'Oauth2 ' + token
},
body: data)
.timeout(Duration(seconds: 5)),
retryIf: (e) => e is TimeoutException || e is SocketException);
print('create reservation is:');
print(response.body);
if (response.statusCode == 200) {
} else if (response.statusCode == 401) {
throw HttpException('401');
} else if (response.statusCode == 500) {
print('server error');
print(response.statusCode);
throw HttpException("Something went wrong");
} else {
print(json.decode(response.body)['detail']);
print(response.statusCode);
throw HttpException(json.decode(response.body)['detail']);
}
} catch (error) {
throw error;
}
}
- 这是 json 请求的主体:
{
"area_id":"282b1837-c052-4ae7-89e5-171787599362",
"number_of_people":1,
"from_time":"2020-09-22 14:00:00",
"to_time":"2020-09-22 14:30:00"
}
解决方案
推荐阅读
- reactjs - 谷歌地图反应不起作用
- facebook - Facebook Messenger bot 被拒绝,因为它不响应非测试人员的个人资料
- python - 使用 csv 数据文件减少代码长度会出现错误 a.all() 或 a.any()
- jasper-reports - 使用 XmlDataSource 在 iReports 中创建表
- ios - 在 XMPPMessageArchiving_Message_CoreDataObject 中存储重复消息(单聊)
- android - 如何使用 Google Fit API 获取各种健身活动的读数?
- html - 使用 CSS 样式表:列宽不正确
- maven - 如何通过jenkins强制部署maven项目..?
- asp.net - 哪个是 .NET Core 中 Kestrel 的根文件夹
- oauth - 带有第三方密钥管理器的 WSO2 API 管理器返回一个立即过期的令牌