skip to Main Content

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.

Update:
For better understanding my problem i have to update my initial description. This code below is working only for 1 Posts of my WordPress rest api result. I need to extend it for all, thats the point where i need help.

Api.dart

  static Future<PostModel> requestPosting() async {
    final result = await httpManager.get(url: posting);
    return PostModel.fromJson(result[0]);
  }

As you can see result[0] pick up the first item, sure i can change it then i get another post but i need all and i am not sure if a loop to iterate through all is the elegant way to handle with wp rest api json file.

cubit.dart

 final response2 =
        await Api.requestPosting(); 
final posting = List.from(response.data ?? []).map((item) {
        return PostModel.fromJson(item);
      }).toList();

Here i need a list of PostModel instances to push it in my Widget ListTitle.

model.dart

 factory PostModel.fromJson(Map<String, dynamic> json) {
    return PostModel(
      date: json["date"] != null ? DateTime.parse(json["date"]) : null,
      id: json['id'] ?? false,
      title: json['title']['rendered'] ?? false,
      content: json['content']['rendered'],
      data: [json]
    );
  }

And here i am setting up the PostingModel. I am not sure if the problem also be here to get all my Posts.

My json request is formatted like in this link, so the normal json wp rest api response: https://jsonplaceholder.typicode.com/posts

3

Answers


  1. You need to use generics so dart know what type are you using. For your function you can change it to:

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

    Api docs for generics: https://dart.dev/guides/language/language-tour#generics

    Additionally

    You can cast it like so as well:

    someList.cast<Map<String, dynamic>>().map<ReturnType>(fn);
    

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

    Login or Signup to reply.
  2. 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();
      

    UPDATE

    at Api.dart

     static Future<List<PostModel>> requestPosting() async {
        final List result = await httpManager.get(url: posting);
        return result.map((item) =>  PostModel.fromJson(item  as Map<String,dynamic>) ).toList();
      } 
    

    at cubit.dart

    final posting = await Api.requestPosting();
    
    Login or Signup to reply.
  3. here you should make a little change -:

     final response2 =
            await Api.requestPosting(); 
    final posting = List.from(response.data ?? []).map((item) {
            return PostModel.fromJson(item as Map<String, dynamic>);
          }).toList();
    

    This will work.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search