Home > OS >  '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'
'_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

Time:08-30

I get a json form POSTMAN and created a file that contains the classes and then with cubit and dio I did the get method also with bloc Provider I called the get method in the main and this error shows up '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String'

And this is how I handle response:

void getFavoritesData() {
     DioHelper.getdata(
       url: FAV,
       token: token,
     ).then((value) {
       favoritesModel = FavoritesModel.fromJson(value.data);       
       print(favoritesModel?.status);
       printFullText(value.data);
       emit(ShopAppGetFavoritesSuccesState());
     }).catchError((error) {
       emit(ShopAppGetFavouritesFailedState());
       print(
           'Error in bringing data from API in the Favorites ${error.toString()}');
     });   }

I checked from all the vaiables and data types and it all correct

class FavoritesModel
{
  late bool status;
  late Data data;

  FavoritesModel.fromJson(Map<String, dynamic> json)
  {
    status = json['status'];
    data = (json['data'] != null ?  Data.fromJson(json['data']) : null)!;
  }
}

class Data
{
 late int currentPage;
 late List<FavoritesData> data=[];
 late String firstPageUrl;
 late int from;
 late int lastPage;
 late String lastPageUrl;
 late String path;
 late int perPage;
 late int to;
 late int total;


  Data.fromJson(Map<String, dynamic> json)
  {
    currentPage = json['current_page'];
    if (json['data'] != null) {
      json['data'].forEach((element) {
        data.add(FavoritesData.fromJson(element));
      });
    }
    firstPageUrl = json['first_page_url'];
    from = json['from'];
    lastPage = json['last_page'];
    lastPageUrl = json['last_page_url'];
    path = json['path'];
    perPage = json['per_page'];
    to = json['to'];
    total = json['total'];
  }


}

class FavoritesData {
  late int id;
  late Product product;

  FavoritesData({required this.id,required this.product});

  FavoritesData.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    product =
    (json['product'] != null ?  Product.fromJson(json['product']) : null)!;
  }


}

class Product {
  late int id;
  late dynamic price;
  late dynamic oldPrice;
  late int discount;
  late String image;
  late String name;
  late String description;



  Product.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    price = json['price'];
    oldPrice = json['old_price'];
    discount = json['discount'];
    image = json['image'];
    name = json['name'];
    description = json['description'];
  }


}

CodePudding user response:

It means you are trying to parse something wrong as you proposed. Which line is throwing error. If you used try catch in bloc comment them and see which line is throwing exception in parsers. If you can’t edit question, add response of request and your repository, cubit parts also.

CodePudding user response:

Do it like this below:

class Data
{
 final int currentPage;

 Data({required this.currentPage});
  factory Data.fromJson(Map<String, dynamic> json)
  {
    return Data(currentPage :json['current_page']);
    
  }

}

CodePudding user response:

Your response value is String not Map, so you need to convert it to map:

void getFavoritesData() {
     DioHelper.getdata(
       url: FAV,
       token: token,
     ).then((value) {
       favoritesModel = FavoritesModel.fromJson(jsonDecode(value.data));// add this     
       print(favoritesModel?.status);
       printFullText(value.data);
       emit(ShopAppGetFavoritesSuccesState());
     }).catchError((error) {
       emit(ShopAppGetFavouritesFailedState());
       print(
           'Error in bringing data from API in the Favorites ${error.toString()}');
     });   }
  • Related