i am trying to fetch data " Unhandled Exception: type ''_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast"
i am using jsonserializable this is my data file i want to use it in product file
CartModel
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);
}
CartResponse
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 Data
I am trying to fetch this json data
[
{
_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
},
]
Error
getCartItem() returning statuscode 200 and also returning Unhandled Exception: errrrrrrrooooorrr type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'String?' in type cast
What is happening here i can't understood
Anybody have any idea?