getter 'id' 沒有為型別 'String' 定義。嘗試匯入定義 'id' 的庫,將名稱更正為現有 getter 的名稱,或者定義一個名為 > 的 getter 或欄位
當我使用購物車圖示添加我的購物車專案時,我試圖顯示它們。當我點擊購物車圖示時,它會將我帶到CartScrren頁面,在那里它會顯示我選擇的專案。但是我無法從我的購物車中獲取值。
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import '../providers/cart.dart' ;
import '../widgets/cart_items_display.dart';
class CartScreen extends StatelessWidget {
static const route = '/cart';
@override
Widget build(BuildContext context) {
final cart = Provider.of<Cart>(context);
return Scaffold(
appBar: AppBar(
title: Text('Your Cart'),
),
body: Column(
children: <Widget>[
Card(
margin: EdgeInsets.all(25),
child: Padding(
padding: EdgeInsets.all(8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: <Widget>[
Text(
'Total',
style: TextStyle(fontSize: 25),
),
Spacer(),
Chip(
label: Text(
'\$${cart.totalAmount}',
style: TextStyle(
color: Colors.white,
),
),
backgroundColor: Theme.of(context).primaryColor,
),
TextButton(onPressed: () {}, child: Text('Order Now')),
],
),
),
),
SizedBox(
height: 10,
),
Expanded(
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (ctx, i) => CartItemDisplay(
//Having problem Here**
cart.items.values.toString()[i].id,
cart.items.values.toString()[i].price,
cart.items.values.toString()[i].quantity,
cart.items.values.toString()[i].title,
///////**
),
),
),
],
),
);
}
}

下面是我的購物車,我正在使用這張地圖。
import 'package:flutter/foundation.dart';
class CartItem {
final String id;
final String title;
final int quantity;
final double price;
CartItem({
required this.id,
required this.title,
required this.quantity,
required this.price,
});
}
class Cart with ChangeNotifier {
Map<String, CartItem> _items ={} ;
Map<String, CartItem> get items {
return {..._items};
}
int get itemCount {
return _items.length;
}
double get totalAmount{
var total=0.0;
_items.forEach((key, cartItem) {
total = cartItem.price*cartItem.quantity;
});
return total;
}
void addItem(String productId, double price, String title) {
if (_items.containsKey(productId)) {
_items.update(
productId,
(existingCartItem) => CartItem(
id: existingCartItem.id,
title: existingCartItem.title,
quantity: existingCartItem.quantity 1,
price: existingCartItem.price));
} else {
_items.putIfAbsent(
productId,
() => CartItem(
id: DateTime.now().toString(),
title: title,
quantity: 1,
price: price,
));
}
notifyListeners();
}
}

但是如果不使用values.toString()它會拋出這個錯誤

uj5u.com熱心網友回復:
請嘗試從購物車值中獲取購物車專案。
final cart = Provider.of<Cart>(context);
final cartItems = cart.items.entries.map((e) => e.value).toList();
...
Expanded(
child: ListView.builder(
itemCount: cartItems.length,
itemBuilder: (ctx, i) => CartItemDisplay(
////* Change these lines.
cartItems[i].id,
cartItems[i].price,
cartItems[i].quantity,
cartItems[i].title,
///////**
),
),
),
...
uj5u.com熱心網友回復:
你使用 getter 來獲取屬性:
class CartItem {
final String id;
final String title;
final int quantity;
final double price;
CartItem({
required this.id,
required this.title,
required this.quantity,
required this.price,
});
String get getId => id;
String get getTitle => title;
int get getQuantity => quantity;
double get getPrice => price;
}
使用封裝來回傳一個屬性總是好的。
Expanded(
child: ListView.builder(
itemCount: cart.items.length,
itemBuilder: (ctx, i) => CartItemDisplay(
//Use getter
cart.items.values.[i].getId,
cart.items.values.[i].getPrice,
cart.items.values.[i].getQuantity.toString(),
cart.items.values.[i].getTitle.toString(),
),
),
),
uj5u.com熱心網友回復:
你可以使用這個站點來構建模型類 https://javiercbk.github.io/json_to_dart/ 我認為這個站點對于創建你的模型類很有用你可以把你的 json 放在這個站點上并獲取模型類
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/375661.html
