Home > Back-end >  List<dynamic> is not a subtype of type 'Map<String, dynamic>
List<dynamic> is not a subtype of type 'Map<String, dynamic>

Time:07-14

i get the following exception:

VERBOSE-2:ui_dart_state.cc(198)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' 
#0 Api.requestPosting (package:listar_flutter_pro/api/api.dart:176:31) <asynchronous suspension> 
#1 HomeCubit.onLoad (package:listar_flutter_pro/blocs/home/home_cubit.dart:18:9)
<asynchronous suspension>

My understanding is that my return send List back but my .map() function to go through each element needs a Map<String, dynamic>. How can i transform the return type to match with my existing function, because i do not want to change the existing .map function.

I appreciate every answer. Thx.

CodePudding user response:

You can cast it like so:

someList.cast<FnParamType>().map<ReturnType>(fn);

Here is the api for this method: https://api.flutter.dev/flutter/dart-core/List/cast.html

CodePudding user response:

your created fromJson method takes a map<String, dynamic> as an argument, but you send to it dynamic comes from List.

then

  1. make sure that response2 is a list of maps

  2. try this PostModel.fromJson(item as Map<String, dynamic>)

     final posting = List.from(response2)
     .map((item) {
         print(response2);
         return PostModel.fromJson(item as Map<String, dynamic>);
      }).toList();
    
  • Related