Home > Net >  flutter : Map method return dynamic not specific object
flutter : Map method return dynamic not specific object

Time:06-19

I get JSON data from Api and with map method I changed map to my specific class :

  final result = await supaService.supabase.client
      .from('user_able')
      .select()
      .eq('user_id', uuid)
      .execute();

  final response = result.data[0]['user'];
  final databaseusers =
      response.map((e) => DataBaseModel.fromJson(e)).toList();

Now I want to return List but I got :

Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'List<DataBaseModel>'

Why? Map method should return list of my object but it returns dynamic?

CodePudding user response:

Try to cast your list:

final databaseusers = response.map((e) => DataBaseModel.fromJson(e)).toList() as List<DataBaseModel>;

Also you can give a type when use the map

final databaseusers = response.map<DataBaseModel>((e) => DataBaseModel.fromJson(e)).toList();
  • Related