错误:flutter/runtime/dart_vm_initializer.cc(41)]未处理的异常:类型“String”不是类型“Map〈String,dynamic>”的子类型

htrmnn0y  于 2022-12-24  发布在  Flutter
关注(0)|答案(1)|浏览(1100)

这是我的密码

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{

    }
  }
}

这是我运行时得到的错误:

\[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\>
yr9zkbsy

yr9zkbsy1#

response.body只是一个String,要将其转换为所需的Map,请调用jsonDecode()

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

相关问题