http - 未处理的异常:键入“列表”' 不是类型转换中类型 'String' 的子类型
问题描述
我正在尝试在我的 api 中发布一个字符串列表。
以下是我的网络代码
class DashboardFeeCountApiProvider implements DashboardFeeCountSource {
@override
Future<DashboardFeeCountModel> getDashboardFees(String type, String userId,
String yearId, List<String> feeCategoryId) async {
final response = await http.post(DASHBOARD_FEE_URL, body: {
"type": "1",
"userid": userId,
"yearId": yearId,
"fee_cat_id": feeCategoryId
});
DashboardFeeCountModel dashboardFeeCountModel =
standardSerializers.deserializeWith(
DashboardFeeCountModel.serializer, json.decode(response.body));
return dashboardFeeCountModel;
}
}
以下是我的集团代码
fetchDashboardFee(String type, String userId, String yearId,
List<String> feeCategoryId) async {
final dashboardFee =
await _repository.getDashboardFees(type, userId, yearId, feeCategoryId);
_dashboardFeeCount.sink.add(dashboardFee);
}
所以在我的主屏幕上,我正在做如下的事情
class _DashboardListState extends State<DashboardList> {
DashboardFeesBloc dashboardFeesBloc;
List<String> categoryListIds = List<String>();
@override
void didChangeDependencies() {
super.didChangeDependencies();
categoryListIds.add("0");
dashboardFeesBloc = DashboardFeesProvider.of(context);
//TODO remov hardcoded values
dashboardFeesBloc.fetchDashboardFee("1","1483", "2", categoryListIds);
}.....
但是每当我提出请求时,我都会收到以下错误
VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: type 'List<String>' is not a subtype of type 'String' in type cast
#0 CastMap.forEach.<anonymous closure> (dart:_internal/cast.dart:286:25)
#1 __InternalLinkedHashMap&_HashVMBase&MapMixin&_LinkedHashMapMixin.forEach (dart:collection/runtime/libcompact_hash.dart:367:8)
#2 CastMap.forEach (dart:_internal/cast.dart:285:13)
#3 mapToQuery (package:http/src/utils.dart:17:7)
#4 Request.bodyFields= (package:http/src/request.dart:128:17)
#5 BaseClient._sendUnstreamed (package:http/src/base_client.dart:163:17)
<asynchronous suspension>
#6 BaseClient.post (package:http/src/base_client.dart:54:7)
#7 post.<anonymous closure> (package:http/http.dart:70:16)
#8 _withClient (package:http/http.dart:166:20)
<asynchronous suspension>
#9 post (package:http/http.dart:69:5)
#10 DashboardFeeCountApiProvider.getDashboardFees (package:dice_admin/resources/dashboard/dashboard_fee_count/dashboard_fee_count_api_provider.d<…>
我也尝试将我的列表转换为字符串,但我不断收到错误。从 Api 角度来看没有问题,因为它在 Postman 中正常工作。
我想我不能直接在请求参数中传递 List 我猜这是导致问题的原因。
解决方案
在带有 http 的 Fluter 应用程序中:
body:{"array":dataArray.join(' ') // transform your List in String}
在服务器端应用程序中:
req.body.array.split(' ') // transform your String in initial Array
推荐阅读
- java - 从 Wicket 外部类中的内部 onClick 方法访问变量
- debugging - pyspark 数据框方法(即 show())无法在 vs 代码调试控制台中打印
- sql-server - SQL Server:需要旋转一些列
- python - 即使在 jupyter notebook 中使用 plt.close(),如何防止 matplotlib 显示图形
- node.js - 有没有办法只得到消息作者的名字
- sql - 跨重复 ID 查找查询中的最新项目
- amazon-web-services - 身份验证的 API 速率限制
- flutter - 颤振:下拉项目以编程方式取消选择问题
- java - PIT 测试说 Minion 由于 TIMED_OUT 异常退出
- python - 未找到任何参数的“详细信息”反向。尝试了 1 种模式:['product/(?P
[-a-zA-Z0-9_]+)$']