简体   繁体   English

未处理的异常:键入“列表”<String> &#39; 不是类型转换中类型 &#39;String&#39; 的子类型

[英]Unhandled Exception: type 'List<String>' is not a subtype of type 'String' in type cast

I am trying to post a list of string in my api.我正在尝试在我的 api 中发布一个字符串列表。

Following is my network code以下是我的网络代码

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;
  }
}

Following is my bloc code以下是我的区块代码

fetchDashboardFee(String type, String userId, String yearId,
      List<String> feeCategoryId) async {
    final dashboardFee =
        await _repository.getDashboardFees(type, userId, yearId, feeCategoryId);
    _dashboardFeeCount.sink.add(dashboardFee);
  }

So in my main screen I am doing something as follows所以在我的主屏幕中,我正在做如下事情

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);

  }.....

but whenever I make a request I get following error但每当我提出请求时,我都会收到以下错误

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<…>

I also tried converting my list to String but I keep getting errors.我也尝试将我的列表转换为字符串,但我不断收到错误消息。 No problem from the Api point as it works properly in Postman. Api 点没有问题,因为它在 Postman 中正常工作。

I think I can't directly pass List in the request parameters I guess which is causing the issue.我想我不能直接在请求参数中传递 List 我猜这是导致问题的原因。

In Fluter app with http:在带有 http 的 Flutter 应用程序中:

body:{"array":dataArray.join(' ') // transform your List in String}

In ServerSide app:在服务器端应用程序中:

req.body.array.split(' ') // transform your String in initial Array

I am able to achieve the output using Dio library我能够使用 Dio 库实现输出

I guess the problem is you can't pass List of String directly using Dart http我想问题是你不能直接使用 Dart http 传递字符串列表

Dio dio =  Dio();

    Map<String, dynamic> body = {
      "type": "1",
      "userid": userId,
      "yearId": yearId,
      "fee_cat_id": feeCategoryId.toList()
    };

    final response = await dio.post(DASHBOARD_FEE_URL, data: body);
    print(response.data.toString());
    DashboardFeeCountModel dashboardFeeCountModel =
    standardSerializers.deserializeWith(
        DashboardFeeCountModel.serializer, response.data);
    return dashboardFeeCountModel;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未处理的异常:键入“列表”<int> &#39; 不是类型转换中类型 &#39;String&#39; 的子类型 - Unhandled Exception: type 'List<int>' is not a subtype of type 'String' in type cast 未处理的异常:类型“int”不是类型转换中“String”类型的子类型 - Unhandled Exception: type 'int' is not a subtype of type 'String' in type cast 未处理的异常:键入“列表”<int> ' 不是类型转换 flutter http 发布请求中的“字符串”类型的子类型</int> - Unhandled Exception: type 'List<int>' is not a subtype of type 'String' in type cast flutter http post request Dart:未处理的异常:“String”类型不是“index”类型“int”的子类型 - Dart : Unhandled exception: type 'String' is not a subtype of type 'int' of 'index' 未处理的异常:类型 '_InternalLinkedHashMap<string, dynamic> ' 不是类型 'Response' 的子类型</string,> - Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Response' 我在颤振/飞镖代码中收到此错误“未处理的异常:输入‘未来’<dynamic> &#39; 不是类型转换中类型 &#39;String&#39; 的子类型” - I am getting this error in my flutter/dart code "Unhandled Exception: type 'Future<dynamic>' is not a subtype of type 'String' in type cast" 错误 HTTP 发布请求未处理异常:键入“列表”<dynamic> ' 不是类型 'Map 的子类型<string, dynamic> '</string,></dynamic> - Error HTTP Post Request Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' Flutter dart json 未处理的异常:InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic></dynamic></string,> - Flutter dart json Unhandled Exception: InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic> 发生异常。 _CastError(类型“_InternalLinkedHashMap<string, dynamic> ' 不是类型 'List 的子类型<dynamic> ' 在类型转换中) uisng getx</dynamic></string,> - Exception has occurred. _CastError (type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>' in type cast) uisng getx 得到错误“键入&#39;列表 <Map<String, Object> &gt;&#39;不是类型转换中&#39;String&#39;类型的子类型” - Getting the error “type 'List<Map<String, Object>>' is not a subtype of type 'String' in type cast”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM