为什么我能';不要在颤振中执行RESTAPI请求
有人能告诉我,当我调用我的为什么我能';不要在颤振中执行RESTAPI请求,rest,flutter,dart,http-post,Rest,Flutter,Dart,Http Post,有人能告诉我,当我调用我的POST请求时,为什么会出现以下异常: Exception has occurred. _CastError (type 'int' is not a subtype of type 'String' in type cast) 这句话的意思是: var response = await http.post(url, body:data); 我用断点检查给出的数据是否正确,它看起来是否正常 我在这一点上被困了好几个小时,不知道下一步该怎么办:/ REST api
POST
请求时,为什么会出现以下异常:
Exception has occurred.
_CastError (type 'int' is not a subtype of type 'String' in type cast)
这句话的意思是:
var response = await http.post(url, body:data);
我用断点检查给出的数据是否正确,它看起来是否正常
我在这一点上被困了好几个小时,不知道下一步该怎么办:/
REST api函数:
addEvent(String title, startDate, startTime, endDate, endTime, description, city, address, entryType, int freePlacesCount) async {
Map data = {
"title":title,
"startDate":startDate,
"startTime":startTime,
"endDate":endDate,
"endTime":endTime,
"description":description,
"city":city,
"address":address,
"entryType":entryType,
"freePlacesCount":freePlacesCount,
//"organizer":organizer
};
var jsonResponse;
var url = 'http://10.0.2.2:80/party';
var response = await http.post(url, body:data);
按钮:
child: RaisedButton(
onPressed: () {
addEvent(
sharedPreferences.getString('eventName'),
sharedPreferences.getString('dateFrom'),
sharedPreferences.getString('timeFrom'),
sharedPreferences.getString('dateTo'),
sharedPreferences.getString('timeTo'),
descriptionText.text,
sharedPreferences.getString('eventCity'),
sharedPreferences.getString('eventAddress'),
sharedPreferences.getString('eventType'),
sharedPreferences.getInt('freePlaces')
);
},
),
问题在于你的身体参数。您只能将
Map
设置为body参数,在您的情况下,它是Map
根据文件:
body
设置请求的正文。它可以是字符串
、列表
或
aMap
要修复它,请将int类型转换为String:
Map<String,String> data = {
"title":title,
"startDate":startDate,
"startTime":startTime,
"endDate":endDate,
"endTime":endTime,
"description":description,
"city":city,
"address":address,
"entryType":entryType,
"freePlacesCount":freePlacesCount.toString(),
};
地图数据={
“头衔”:头衔,
“开始日期”:开始日期,
“开始时间”:开始时间,
“结束日期”:结束日期,
“结束时间”:结束时间,
“描述”:描述,
“城市”:城市,
“地址”:地址,
“entryType”:entryType,
“FreeplaceScont”:FreeplaceScont.toString(),
};
这是您的API返回的错误?不,这是来自的异常flutter@FPerroch我添加了完全例外的屏幕当你发表文章时你的API被调用了吗?我不明白你为什么会犯这样的错误。是的,我必须提供freePlacesCount
,就像一个整数而不是一个整数一样string@Piotrek:如您所见,这在当前受支持的主体参数集中是不可能的。你至少试过上面的代码了吗?当您发布上述字符串数据时,您的服务器失败了吗?是的,我尝试了,从服务器返回错误的请求为什么我不能使用Map
?@Piotrek检查文档,它是这样设计的。您可以询问开发人员或在他们的github回购上发布问题