http - Flutter - 向我的服务器发出 HTTP 发布请求时出错
问题描述
所以我有这个 Future 函数,旨在向我的服务器发出 HTTP 请求:
Future getReviewsComments(List reviewIDs) async {
Map data = {
"reviewIDs": [reviewIDs]
};
http.Response response = await http.post(
Uri.encodeFull(config.domain + '/getReviewsComments'),
body: data
);
if (response.statusCode != 200){
return false;
}
return json.decode(response.body);
}
该函数在我的函数中运行,initState
例如:
void initState(){
List reviewIDs = ["5c4962b37d6b5f50146b8df9", "5c4966901bd9c3141c2f4700"];
eventActions.getReviewsComments(reviewIDs).then(
(comments){
print( "WORKDED");
}
);
super.initState();
}
但是当我运行应用程序时,我收到了这个错误:
E/flutter ( 7567): [ERROR:flutter/shell/common/shell.cc(186)] Dart Error: Unhandled exception:
E/flutter ( 7567): type 'List<dynamic>' is not a subtype of type 'String' in type cast
E/flutter ( 7567): #0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:286:25)
E/flutter ( 7567): #1 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:367:8)
E/flutter ( 7567): #2 CastMap.forEach (dart:_internal/cast.dart:285:13)
E/flutter ( 7567): #3 mapToQuery
package:http/src/utils.dart:17
E/flutter ( 7567): #4 Request.bodyFields=
...
请问如何解决这个问题?
注意:print(reviewIDs)
退货[5c4962b37d6b5f50146b8df9, 5c4966901bd9c3141c2f4700]
解决方案
body
ofhttp.post
只能是以下之一:
- 一个字节数组作为
List<int>
- 一个字符串,将通过 UTF-8 编码将其转换为字节数组
- A
Map<String, String>
将被编码为 HTML 表单数据,即 x-www-form-urlencoded
您正在传递 a Map<String, List<String>>
,这不是上述内容。你的服务器需要什么?也许是一个 json 编码的字符串?(如果是这样,请使用json.encode(data)
。)
推荐阅读
- php - 使用 PHP 删除之前和之后的所有字符
- sql - Psql - '数据库不存在',而它是在 Valentina Studio 中创建的
- fisheye - 将 360 - 鱼眼镜头转换为 4 个普通 - 方形视频 - 中国鱼眼
- python - Python RexExpression - 识别标准
- npm - yo @microsoft/sharepoint 在此生成器上失败 (@microsoft/sharepoint:app) 需要 yeoman-environment 至少 3.0.0,当前版本是 2.10.3
- amazon-cloudtrail - AWS KMS 密钥策略未授予 CloudTrail 足够的访问多个账户的权限
- sql - 在表列中查找每组最频繁的值
- ruby-on-rails - 是否应允许非机密客户端应用程序使用 OAuth 2.0 客户端凭据流?
- java - Spring 安全自定义过滤器(在 AuthenticationManager bean 已经存在时创建它的原因)
- azure - 如何通过 azure ML 服务中的 azure 容器实例部署 colab 训练的模型?