出现错误:字符串类型不是动态映射字符串类型的子类型
import 'package:ajooyo/features/products/models/product_model.dart';
class CartItem {
final Product product;
final int quantity;
final String id;
CartItem({
required this.product,
required this.quantity,
required this.id,
});
factory CartItem.fromJson(Map<String, dynamic> json) {
return CartItem(
product: Product.fromJson(json['product']),
quantity: json['quantity'],
id: json['_id'],
);
}
}
Cart model
import 'package:ajooyo/features/products/models/product_cart_item_model.dart';
class Cart {
final String id;
final String userId;
final List<CartItem> items;
Cart({
required this.id,
required this.userId,
required this.items,
});
factory Cart.fromJson(Map<String, dynamic> json) {
return Cart(
id: json['_id'] as String,
userId: json['user'] as String,
items: (json['items'] as List)
.map((item) => CartItem.fromJson(item as Map<String,
dynamic>)).toList(),
);
}
}
这是来自后端的json文件
{
"updatedCart": {
"_id": "66442f4",
"user": "6628996ba",
"items": [
{
"product": {
"_id": "66496e7e",
"user": "6696ba",
"vendor": "66311944",
"productName": "Yemlays Men's Fashion Comfortable Hoodie-black",
"productBrandName": "Yemlays",
"productPrice": "24,360",
"productDescription": "Welcome to our shop.",
"productDaysOfDelivery": "3",
"productType": "Clothing",
"productSize": "Regular",
"productQuantity": "",
"productImages": [
{
"productUrl": "https://_image/td3tadqj2n52vpelxntk.jpg",
"_id": "6640c8b824675c8aaa196e7f"
}
],
"productAvailability": true,
"productStatus": "active",
"createdAt": "2024-05-12T13:48:40.612Z",
"updatedAt": "2024-05-12T13:48:40.612Z",
"__v": 0
},
"quantity": 2,
"_id": "664454addc21f61eb83252f7"
},
{
"product": {
"_id": "66403",
"user": "66ba",
"vendor": "66310e944",
"productName": "Yemlays Martin Boots Men's Tooling All-match High-top Shoes-beige",
"productBrandName": "Yemlays",
"productPrice": "9,467",
"productDescription": "This pair in addition to.",
"productDaysOfDelivery": "3",
"productType": "Foot Wares",
"productSize": "44",
"productQuantity": "",
"productImages": [
{
"productUrl": "https://t_image/xalew0pxva25ly49xb1w.jpg",
"_id": "6640c86224675c8aaa196e74"
},
{
"productUrl": "https://r9w.jpg",
"_id": "6640c86224675c8aaa196e75"
},
{
"productUrl": "https://re.jpg",
"_id": "6640c86224675c8aaa196e76"
},
{
"productUrl": "https://repg",
"_id": "6640c86224675c8aaa196e77"
}
],
"productAvailability": true,
"productStatus": "active",
"createdAt": "2024-05-12T13:47:14.889Z",
"updatedAt": "2024-05-12T13:47:14.889Z",
"__v": 0
},
"quantity": 1,
"_id": "664455b9dc21f61eb8325313"
}
],
"__v": 1
}
}
我希望它能够映射数据,但我继续从 Json 获取数据 但在下面的代码中它打印了
Response data is a Map;
这意味着 api 数据是 Map 数据,
同样对于这个 print("Response type: ${response.data.runtimeType}");,它打印了 Response type: _Map<String, dynamic>
所以我真的不知道错误来自哪里......
Future<Cart?> addProductToCart(String productId, int quantity) async {
try {
const endpoint = "user/add_product_cart";
final response = await apiProvider.post(endpoint, data: {
'productId': productId,
'quantity': quantity,
});
// Debugging information
print("Response type: ${response.data.runtimeType}");
print("Response data: ${response.data}");
if (response.data is Map<String, dynamic>) {
print("Response data is a Map");
final updatedCartData =
response.data['updatedCart'] as Map<String, dynamic>;
print("updatedCart data type: ${updatedCartData.runtimeType}");
return Cart.fromJson(updatedCartData);
} else {
print("Response data is not a Map");
throw Exception('Unexpected response format');
}
} catch (e) {
print("Error: $e");
print('Error occures here');
rethrow;
// throw Exception('Failed to add product to cart: $e');
}
}
我想你的模型不适合 json 数据。您可以通过以下网站将 json 数据转换为类:https://javiercbk.github.io/json_to_dart/
我将你的 json 数据转换为模型和结果,如下所示:
class CartItem {
UpdatedCart? updatedCart;
CartItem({this.updatedCart});
CartItem.fromJson(Map<String, dynamic> json) {
updatedCart = json['updatedCart'] != null
? UpdatedCart.fromJson(json['updatedCart'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (updatedCart != null) {
data['updatedCart'] = updatedCart!.toJson();
}
return data;
}
}
class UpdatedCart {
String? sId;
String? user;
List<Items>? items;
int? iV;
UpdatedCart({this.sId, this.user, this.items, this.iV});
UpdatedCart.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
user = json['user'];
if (json['items'] != null) {
items = <Items>[];
json['items'].forEach((v) {
items!.add(Items.fromJson(v));
});
}
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['_id'] = sId;
data['user'] = user;
if (items != null) {
data['items'] = items!.map((v) => v.toJson()).toList();
}
data['__v'] = iV;
return data;
}
}
class Items {
Product? product;
int? quantity;
String? sId;
Items({this.product, this.quantity, this.sId});
Items.fromJson(Map<String, dynamic> json) {
product =
json['product'] != null ? Product.fromJson(json['product']) : null;
quantity = json['quantity'];
sId = json['_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (product != null) {
data['product'] = product!.toJson();
}
data['quantity'] = quantity;
data['_id'] = sId;
return data;
}
}
class Product {
String? sId;
String? user;
String? vendor;
String? productName;
String? productBrandName;
String? productPrice;
String? productDescription;
String? productDaysOfDelivery;
String? productType;
String? productSize;
String? productQuantity;
List<ProductImages>? productImages;
bool? productAvailability;
String? productStatus;
String? createdAt;
String? updatedAt;
int? iV;
Product(
{this.sId,
this.user,
this.vendor,
this.productName,
this.productBrandName,
this.productPrice,
this.productDescription,
this.productDaysOfDelivery,
this.productType,
this.productSize,
this.productQuantity,
this.productImages,
this.productAvailability,
this.productStatus,
this.createdAt,
this.updatedAt,
this.iV});
Product.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
user = json['user'];
vendor = json['vendor'];
productName = json['productName'];
productBrandName = json['productBrandName'];
productPrice = json['productPrice'];
productDescription = json['productDescription'];
productDaysOfDelivery = json['productDaysOfDelivery'];
productType = json['productType'];
productSize = json['productSize'];
productQuantity = json['productQuantity'];
if (json['productImages'] != null) {
productImages = <ProductImages>[];
json['productImages'].forEach((v) {
productImages!.add(ProductImages.fromJson(v));
});
}
productAvailability = json['productAvailability'];
productStatus = json['productStatus'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['_id'] = sId;
data['user'] = user;
data['vendor'] = vendor;
data['productName'] = productName;
data['productBrandName'] = productBrandName;
data['productPrice'] = productPrice;
data['productDescription'] = productDescription;
data['productDaysOfDelivery'] = productDaysOfDelivery;
data['productType'] = productType;
data['productSize'] = productSize;
data['productQuantity'] = productQuantity;
if (productImages != null) {
data['productImages'] = productImages!.map((v) => v.toJson()).toList();
}
data['productAvailability'] = productAvailability;
data['productStatus'] = productStatus;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
data['__v'] = iV;
return data;
}
}
class ProductImages {
String? productUrl;
String? sId;
ProductImages({this.productUrl, this.sId});
ProductImages.fromJson(Map<String, dynamic> json) {
productUrl = json['productUrl'];
sId = json['_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['productUrl'] = productUrl;
data['_id'] = sId;
return data;
}
}
我希望它能够映射数据,但我不断获取数据 fromJson 但在下面的代码中它打印了 Response data is a Map; 这意味着 api 数据是 Map 数据...
正确,整个响应是一张地图。
但是,
type String is not a subtype of type Map<String, dynamic>
的误差可以与主响应无关,它可以是主响应的子类型之一,例如Product
或其任何子类型。在某个地方,响应中的对象(地图)被反序列化为字符串。
解决此类问题的一个好方法是为响应中的每个对象创建一个新模型(子类型):
class ResponseModel {
UpdatedCart? updatedCart;
ResponseModel({this.updatedCart});
ResponseModel.fromJson(Map<String, dynamic> json) {
updatedCart = json['updatedCart'] != null
? UpdatedCart.fromJson(json['updatedCart'])
: null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (updatedCart != null) {
data['updatedCart'] = updatedCart!.toJson();
}
return data;
}
}
class UpdatedCart {
String? sId;
String? user;
List<Items>? items;
int? iV;
UpdatedCart({this.sId, this.user, this.items, this.iV});
UpdatedCart.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
user = json['user'];
if (json['items'] != null) {
items = <Items>[];
json['items'].forEach((v) {
items!.add(Items.fromJson(v));
});
}
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['_id'] = sId;
data['user'] = user;
if (items != null) {
data['items'] = items!.map((v) => v.toJson()).toList();
}
data['__v'] = iV;
return data;
}
}
class Items {
Product? product;
int? quantity;
String? sId;
Items({this.product, this.quantity, this.sId});
Items.fromJson(Map<String, dynamic> json) {
product =
json['product'] != null ? Product.fromJson(json['product']) : null;
quantity = json['quantity'];
sId = json['_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
if (product != null) {
data['product'] = product!.toJson();
}
data['quantity'] = quantity;
data['_id'] = sId;
return data;
}
}
class Product {
String? sId;
String? user;
String? vendor;
String? productName;
String? productBrandName;
String? productPrice;
String? productDescription;
String? productDaysOfDelivery;
String? productType;
String? productSize;
String? productQuantity;
List<ProductImages>? productImages;
bool? productAvailability;
String? productStatus;
String? createdAt;
String? updatedAt;
int? iV;
Product(
{this.sId,
this.user,
this.vendor,
this.productName,
this.productBrandName,
this.productPrice,
this.productDescription,
this.productDaysOfDelivery,
this.productType,
this.productSize,
this.productQuantity,
this.productImages,
this.productAvailability,
this.productStatus,
this.createdAt,
this.updatedAt,
this.iV});
Product.fromJson(Map<String, dynamic> json) {
sId = json['_id'];
user = json['user'];
vendor = json['vendor'];
productName = json['productName'];
productBrandName = json['productBrandName'];
productPrice = json['productPrice'];
productDescription = json['productDescription'];
productDaysOfDelivery = json['productDaysOfDelivery'];
productType = json['productType'];
productSize = json['productSize'];
productQuantity = json['productQuantity'];
if (json['productImages'] != null) {
productImages = <ProductImages>[];
json['productImages'].forEach((v) {
productImages!.add(ProductImages.fromJson(v));
});
}
productAvailability = json['productAvailability'];
productStatus = json['productStatus'];
createdAt = json['createdAt'];
updatedAt = json['updatedAt'];
iV = json['__v'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['_id'] = sId;
data['user'] = user;
data['vendor'] = vendor;
data['productName'] = productName;
data['productBrandName'] = productBrandName;
data['productPrice'] = productPrice;
data['productDescription'] = productDescription;
data['productDaysOfDelivery'] = productDaysOfDelivery;
data['productType'] = productType;
data['productSize'] = productSize;
data['productQuantity'] = productQuantity;
if (productImages != null) {
data['productImages'] =
productImages!.map((v) => v.toJson()).toList();
}
data['productAvailability'] = productAvailability;
data['productStatus'] = productStatus;
data['createdAt'] = createdAt;
data['updatedAt'] = updatedAt;
data['__v'] = iV;
return data;
}
}
class ProductImages {
String? productUrl;
String? sId;
ProductImages({this.productUrl, this.sId});
ProductImages.fromJson(Map<String, dynamic> json) {
productUrl = json['productUrl'];
sId = json['_id'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = <String, dynamic>{};
data['productUrl'] = productUrl;
data['_id'] = sId;
return data;
}
}
ResponseModel
是解析整个响应的输出,其中包含响应中每个对象的子类型。