我收到如下错误 "Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast"
i got this error "Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast" as below
[这是错误的截图]
**这是我用来从服务器获取数据的提供程序代码,出现错误未处理异常:类型 'String' 不是类型转换中类型 'Map' 的子类型:* ****
Future<void> fetchUserOrders () async{
final prefs = await SharedPreferences.getInstance();
String accessToken = prefs.getString(Constants.prefsUserAccessTokenKey);
print("accessToken :" +accessToken);
final url = Urls.fetchUserOrders;
final response = await retry(()=>
http.get(url, headers: {
"Accept": "application/json",
"Authorization": "Bearer " + accessToken
}
).timeout(Duration(seconds: 10)),
retryIf: (error)=> error is SocketException || error is TimeoutException,
);
final List<Order> loadedOrders = [];
final extractData = json.encode(response.body) as Map<String, dynamic>;
extractData.forEach((orderId, orderData){
loadedOrders.add(Order.fromJson(extractData));
});
_orders = loadedOrders;
notifyListeners();
}
这是我用来处理获取请求的模型代码
进口'order_item.dart';
class Order {
int id;
String userName;
String userMobile;
int numberOfItems;
String orderSummary;
int price;
String currentStatus;
String address;
int vatAmount;
int serviceTax;
int delivery;
int discountValue;
String restaurantName;
int restaurantId;
String branchName;
List<OrderItem> orderItems;
String orderComment;
bool inBranch;
Order(
{this.id,
this.userName,
this.userMobile,
this.numberOfItems,
this.orderSummary,
this.price,
this.currentStatus,
this.address,
this.vatAmount,
this.serviceTax,
this.delivery,
this.discountValue,
this.restaurantName,
this.restaurantId,
this.branchName,
this.orderItems,
this.orderComment,
this.inBranch});
******************************
从服务器处理数据
Order.fromJson(Map<String, dynamic> json) {
id = json['Id'];
userName = json['UserName'];
userMobile = json['UserMobile'];
numberOfItems = json['NumberOfItems'];
orderSummary = json['OrderSummary'];
price = json['Price'];
currentStatus = json['CurrentStatus'];
address = json['Address'];
vatAmount = json['VatAmount'];
serviceTax = json['ServiceTax'];
delivery = json['Delivery'];
discountValue = json['DiscountValue'];
restaurantName = json['RestaurantName'];
restaurantId = json['RestaurantId'];
branchName = json['BranchName'];
if (json['orderItems'] != null) {
orderItems = new List<OrderItem>();
json['orderItems'].forEach((v) {
orderItems.add(new OrderItem.fromJson(v));
});
}
orderComment = json['OrderComment'];
inBranch = json['InBranch'];
}
将数据 post 发送到服务器
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Id'] = this.id;
data['UserName'] = this.userName;
data['UserMobile'] = this.userMobile;
data['NumberOfItems'] = this.numberOfItems;
data['OrderSummary'] = this.orderSummary;
data['Price'] = this.price;
data['CurrentStatus'] = this.currentStatus;
data['Address'] = this.address;
data['VatAmount'] = this.vatAmount;
data['ServiceTax'] = this.serviceTax;
data['Delivery'] = this.delivery;
data['DiscountValue'] = this.discountValue;
data['RestaurantName'] = this.restaurantName;
data['RestaurantId'] = this.restaurantId;
data['BranchName'] = this.branchName;
if (this.orderItems != null) {
data['orderItems'] = this.orderItems.map((v) => v.toJson()).toList();
}
data['OrderComment'] = this.orderComment;
data['InBranch'] = this.inBranch;
return data;
}
}
final extractData = json.encode(response.body) as Map<String, dynamic>;
应该是
final extractData = json.decode(response.body) as Map<String, dynamic>;
[这是错误的截图]
**这是我用来从服务器获取数据的提供程序代码,出现错误未处理异常:类型 'String' 不是类型转换中类型 'Map' 的子类型:* ****
Future<void> fetchUserOrders () async{
final prefs = await SharedPreferences.getInstance();
String accessToken = prefs.getString(Constants.prefsUserAccessTokenKey);
print("accessToken :" +accessToken);
final url = Urls.fetchUserOrders;
final response = await retry(()=>
http.get(url, headers: {
"Accept": "application/json",
"Authorization": "Bearer " + accessToken
}
).timeout(Duration(seconds: 10)),
retryIf: (error)=> error is SocketException || error is TimeoutException,
);
final List<Order> loadedOrders = [];
final extractData = json.encode(response.body) as Map<String, dynamic>;
extractData.forEach((orderId, orderData){
loadedOrders.add(Order.fromJson(extractData));
});
_orders = loadedOrders;
notifyListeners();
}
这是我用来处理获取请求的模型代码
进口'order_item.dart';
class Order {
int id;
String userName;
String userMobile;
int numberOfItems;
String orderSummary;
int price;
String currentStatus;
String address;
int vatAmount;
int serviceTax;
int delivery;
int discountValue;
String restaurantName;
int restaurantId;
String branchName;
List<OrderItem> orderItems;
String orderComment;
bool inBranch;
Order(
{this.id,
this.userName,
this.userMobile,
this.numberOfItems,
this.orderSummary,
this.price,
this.currentStatus,
this.address,
this.vatAmount,
this.serviceTax,
this.delivery,
this.discountValue,
this.restaurantName,
this.restaurantId,
this.branchName,
this.orderItems,
this.orderComment,
this.inBranch});
******************************
从服务器处理数据
Order.fromJson(Map<String, dynamic> json) {
id = json['Id'];
userName = json['UserName'];
userMobile = json['UserMobile'];
numberOfItems = json['NumberOfItems'];
orderSummary = json['OrderSummary'];
price = json['Price'];
currentStatus = json['CurrentStatus'];
address = json['Address'];
vatAmount = json['VatAmount'];
serviceTax = json['ServiceTax'];
delivery = json['Delivery'];
discountValue = json['DiscountValue'];
restaurantName = json['RestaurantName'];
restaurantId = json['RestaurantId'];
branchName = json['BranchName'];
if (json['orderItems'] != null) {
orderItems = new List<OrderItem>();
json['orderItems'].forEach((v) {
orderItems.add(new OrderItem.fromJson(v));
});
}
orderComment = json['OrderComment'];
inBranch = json['InBranch'];
}
将数据 post 发送到服务器
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Id'] = this.id;
data['UserName'] = this.userName;
data['UserMobile'] = this.userMobile;
data['NumberOfItems'] = this.numberOfItems;
data['OrderSummary'] = this.orderSummary;
data['Price'] = this.price;
data['CurrentStatus'] = this.currentStatus;
data['Address'] = this.address;
data['VatAmount'] = this.vatAmount;
data['ServiceTax'] = this.serviceTax;
data['Delivery'] = this.delivery;
data['DiscountValue'] = this.discountValue;
data['RestaurantName'] = this.restaurantName;
data['RestaurantId'] = this.restaurantId;
data['BranchName'] = this.branchName;
if (this.orderItems != null) {
data['orderItems'] = this.orderItems.map((v) => v.toJson()).toList();
}
data['OrderComment'] = this.orderComment;
data['InBranch'] = this.inBranch;
return data;
}
}
final extractData = json.encode(response.body) as Map<String, dynamic>;
应该是
final extractData = json.decode(response.body) as Map<String, dynamic>;