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
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);
}
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");
}
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
},
]
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?
The problem is in your conversion. It seems you need to convert the JSON to a list. However, the code returns Iterable. To return a list do the following. And one more piece of advice. If you know what type of data should be returned, do not use var
instead use the type that you are expecting such that List<AddToCart>
this will give you a more specific error about the code.
List<AddToCart> myCartItems = CartListResponse.fromJson(jsonDecode(response.body)).toList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.