i got this error "Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' in type cast" as below
426
final extractData = json.encode(response.body) as Map<String, dynamic>;
should be
final extractData = json.decode(response.body) as Map<String, dynamic>;
Author by
Ahmad Mohy
Updated on December 20, 2022Comments
-
Ahmad Mohy over 1 year
[this is a snapshot from the error]
**this is the provider code which i use to fetch data from server and there is an error Unhandled Exception: type 'String' is not a subtype of type 'Map' in type cast:*****
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(); }
this is the model code that i use to handel the get request
import '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}); ******************************
to handel data from server
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']; }
to handel data post to a server
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; } }