這可能是一個基本問題,但我正在嘗試從下面的 JSON 資料中計算稅收。但是,每次嘗試列印結果時都會收到此訊息。
未捕獲的錯誤:TypeError:“JsLinkedHashMap<String, Object>”的實體:型別“JsLinkedHashMap<String, Object>”不是“int”型別的子型別
void main() {
double tax = 0.0;
int index = 0;
Map<String, dynamic> test = {
"data": {
"cartItem": [
{
"id": 30,
"productName": "Mango",
"price": 100.0,
"tax": 10.0,
"quantity": 7,
"mainImage": "http://3.109.206.91:8000/static/product_module/product/png-clipart-slice-of-mango-mango-tea-fruit-mango-game-food_HSPnZGK.png",
"totalPrice": 700.0
}
],
"grandTotal": 700.0
}
};
for(index in test['data']['cartItem']) {
tax = (test['data']['cartItem'][index]['tax'] / 100.0);
}
print(tax);
}
運行回圈背后的原因是串列中可能有多個物件data。
uj5u.com熱心網友回復:
您的代碼不正確。
void main() {
double tax = 0.0;
int index = 0;
Map<String, dynamic> test = {
"data": {
"cartItem": [
{
"id": 30,
"productName": "Mango",
"price": 10.0,
"tax": 10.0,
"quantity": 7,
"mainImage":
"http://3.109.206.91:8000/static/product_module/product/png-clipart-slice-of-mango-mango-tea-fruit-mango-game-food_HSPnZGK.png",
"totalPrice": 700.0
}
],
"grandTotal": 700.0
}
};
for (final cartItem in test['data']['cartItem']) {
tax = (cartItem['tax'] as num) / 100.0;
}
print(tax);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475425.html
標籤:镖
上一篇:如何讓React測驗庫的userEvent輸入到輸入中
下一篇:Fluttersrc/C:/src/flutter/.pub-cache/hosted/pub.dartlang.org/get-4.6.3/lib/get_navigation/src/中的錯誤
