简体   繁体   中英

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.

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.

I think I can't directly pass List in the request parameters I guess which is causing the issue.

In Fluter app with http:

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

I guess the problem is you can't pass List of String directly using 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM