Home > Mobile >  ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is no
ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type 'String' is no

Time:12-22

This is my code

class PopularProductController extends GetxController {
  final PopularProductsRepo popularProductsRepo;

  PopularProductController({required this.popularProductsRepo});

  List<dynamic> _popularProductList = [];

  List <dynamic> get popularProductList => _popularProductList;

  Future<void> getPopularProductList()async {
    Response response = await popularProductsRepo.getPopularProductList();
    if(response.statusCode==200) {
     print("get products");
      _popularProductList =[];
      _popularProductList.addAll(Product.fromJson(response.body).products);// buradaaaaaaaaaaa
      print(_popularProductList.length);
      update();
    }else{


    }
  }
}

and this is the error I got when it runs:

\[GETX\] Instance "ApiClient" has been initialized

\[GETX\] Instance "PopularProductsRepo" has been initialized

\[GETX\] Instance "PopularProductController" has been initialized

\[GETX\] Instance "GetMaterialController" has been created

\[GETX\] Instance "GetMaterialController" has been initialized

D/EGL_emulation(17836): app_time_stats: avg=17514.20ms min=55.20ms max=51897.92ms count=3

I/flutter (17836): get products

E/flutter (17836): \[ERROR:flutter/runtime/dart_vm_initializer.cc(41)\] Unhandled Exception: type 
'String' is not a subtype of type 'Map\<String, dynamic\>'

E/flutter (17836): #0      PopularProductController.getPopularProductList 

(package:food_app/controllers/popular_product_controller.dart:19:60)

E/flutter (17836): \<asynchronous suspension\>

E/flutter (17836):

I/flutter (17836): get products

E/flutter (17836): \[ERROR:flutter/runtime/dart_vm_initializer.cc(41)\] Unhandled Exception: type 
'String' is not a subtype of type 'Map\<String, dynamic\>'

E/flutter (17836): #0      PopularProductController.getPopularProductList 

(package:food_app/controllers/popular_product_controller.dart:19:60)

E/flutter (17836): \<asynchronous suspension\>

CodePudding user response:

response.body is just a String. To transform it into the Map you need, call jsonDecode():

_popularProductList.addAll(Product.fromJson(jsonDecode(response.body)).products);
  • Related