未处理的异常:'_InternalLinkedHashMap<String, dynamic>' 不是类型转换中类型 'String?' 的子类型

Unhandled Exception: '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast

我正在尝试获取数据“未处理的异常:类型 ''_InternalLinkedHashMap' 不是类型转换中类型 'String?' 的子类型”

我正在使用json可序列化这是我的数据文件我想在产品文件中使用它

购物车模型

import 'package:json_annotation/json_annotation.dart';

part 'addToCartModel.g.dart';


@JsonSerializable()
class AddToCart {
  @JsonKey(name:'_id')
  final String? id;
  final String? productQuantity;
  final String? product;
  final String? cartAddedBy;


  AddToCart({
    this.id,
    this.productQuantity, 
    this.product, 
    this.cartAddedBy, 
    });


  factory AddToCart.fromJson(Map<String, dynamic> json) => _$AddToCartFromJson(json);
  Map<String, dynamic> toJson() => _$AddToCartToJson(this);
}

购物车响应

import 'package:json_annotation/json_annotation.dart';

part 'addToCart_response.g.dart';



@JsonSerializable(explicitToJson: true)
class CartListResponse {
  bool success;
  final List<AddToCart> data;

  CartListResponse({    
    required this.success, 
    required this.data
  });

  factory CartListResponse.fromJson(Map<String, dynamic> json) => 
    _$CartListResponseFromJson(json);

  Map<String, dynamic> toJson() => _$CartListResponseToJson(this);
}

httpCart

  Future<List<AddToCart>> getCartItem() async{
    final SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
    var token = sharedPreferences.getString("token")?? "null";
    String tok = 'Bearer $token';
    try {
      final response = await get(Uri.parse(My_CART), headers: {'Authorization': tok});
      if (response.statusCode==200) {
          var myCartItems = CartListResponse.fromJson(jsonDecode(response.body));
          return myCartItems.data;
      }
      else{
        throw Exception("Failed to load cart items");
      }
      
    } catch (e) {
        Future.error("errrrrrrrooooorrr $e");
    }
    return throw Exception("Failed to connect server");

    
  }

Json数据

我正在尝试获取此 json 数据

[
    {
      _id: new ObjectId("621b749372e7f526c4557e3a"),
      productQuantity: '1',
      product: {
        _id: new ObjectId("6217da21f931916d5948eb6e"),
        productName: 'Test Product',
        productDesc: 'T-shirt for men',
        productThumbnail: 'media/1645730337059tshirt.jpg',
        productDisplayPrice: 1500,
        productActualPrice: 1000,
        adminId: new ObjectId("6217d9fcf931916d5948eb6a"),
        createdAt: 2022-02-24T19:18:57.082Z,
        __v: 0
      },
      cartAddedBy: new ObjectId("62167834e94669f79bb2e29c"),
      __v: 0
    },
    {
        _id: new ObjectId("621b749372e7f526c4557e3a"),
        productQuantity: '1',
        product: {
          _id: new ObjectId("6217da21f931916d5948eb6e"),
          productName: 'Test Product',
          productDesc: 'T-shirt for men',
          productThumbnail: 'media/1645730337059tshirt.jpg',
          productDisplayPrice: 1500,
          productActualPrice: 1000,
          adminId: new ObjectId("6217d9fcf931916d5948eb6a"),
          createdAt: 2022-02-24T19:18:57.082Z,
          __v: 0
        },
        cartAddedBy: new ObjectId("62167834e94669f79bb2e29c"),
        __v: 0
      },

]
  

错误

getCartItem() returning statuscode 200 and also returning Unhandled Exception: errrrrrrrooooorrr type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast

这里发生了什么我不明白

有人知道吗?

问题出在你的转换上。看来您需要将 JSON 转换为列表。但是,代码 returns 是可迭代的。要 return 列表,请执行以下操作。还有一条建议。如果您知道应该 returned 什么类型的数据,请不要使用 var 而是使用您期望的类型,这样 List<AddToCart> 这会给您一个关于代码的更具体的错误.

 List<AddToCart> myCartItems = CartListResponse.fromJson(jsonDecode(response.body)).toList();