我使用提供包(状态管理)创建了一个购物车应用,用于在购物车页面中存储所选商品或产品,但是问题是我收到“类型为String的错误,不是类型为Item的子类型”。下面是飞镖代码和模型,如果我尝试单击主页上的任何项目,则将所选项目存储在购物车页面中;如果我尝试存储第二个页面(数量页面)中的项目,则会出现错误。
HomePage.dart
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();}
class _HomeState extends State<Home> {
List<Item> _product=[
Item(
title: "Cake",
image: "assets/1.png",
price: 20.00,
),
Item(
title: "Pasteries",
image: "assets/2.png",
price: 30.00,
),];
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return PlatformScaffold(
body: ListView.builder(
itemCount: _product.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.only(
top: 35.0, bottom: 15.0, left: 20.0, right: 20.0),
child: GestureDetector(
onTap: (){
//cart.add(_product[index]);//Here i try to select the item and it successfully stored in cart page
Navigator.of(context).push(MaterialPageRoute(builder: (context) => Quantities(
productname: _product[index].title,
productprice: _product[index].price,
productimage: _product[index].image,
)));},
child: Container(
child: new FittedBox(
child: Material(
color: Colors.white,
elevation: 15.0,
borderRadius: BorderRadius.circular(15.0),
shadowColor: Color(0x802196F3),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Container(
width: 250,
height: 200,
child: ClipRRect(
borderRadius: new BorderRadius.circular(15.0),
child: new Image.asset(
_product[index].image,
fit: BoxFit.cover,),),),
Padding(
padding: const EdgeInsets.only(top: 5.0,bottom: 5.0),
child: Text(_product[index].title,style: TextStyle(color: Colors.blueGrey[700],
fontWeight: FontWeight.bold,fontSize: 18.0),),
),],)),),),),);}));},);}}
Quantities.dart
class Quantities extends StatefulWidget {
var productprice;
String productimage;
final productname;
Quantities({this.productprice, this.productimage, this.productname});
@override
_QuantitiesState createState() => _QuantitiesState(productprice,productimage,productname);}
class _QuantitiesState extends State<Quantities> {
final productprice;
final productimage;
final productname;
_QuantitiesState(this.productprice, this.productimage, this.productname);
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return PlatformScaffold(
appBar: PlatformAppBar(
backgroundColor: Colors.lightBlue[900],
title: Text('Details'),),
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Container(
child: Column(
children: <Widget>[
Container(
height: 150.0,
child: GridTile(
child: Container(
color: Colors.white,
child: Image.asset(productimage),),),),
Text(productname,style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
Text("Price: "+productprice.toString()+" SAR",style: TextStyle(fontWeight: FontWeight.bold, fontSize: 20.0),),
Padding(
padding: const EdgeInsets.only(top: 20.0),
child: Center(
child: PlatformButton(
onPressed: () {
cart.add(productname);},// Here i am getting an error
child: Text('Add to Cart',style: TextStyle(color: Colors.white),),
androidFlat: (_) => MaterialFlatButtonData(
color: Colors.cyan),
ios: (_) => CupertinoButtonData(
color: Colors.cyan
)),),),],),),),],),);},);}}
CartPage.dart
class CartPage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return _CartPageState();}}
class _CartPageState extends State<CartPage> {
@override
Widget build(BuildContext context) {
return Consumer<Cart>(
builder: (context,cart,child){
return Scaffold(
appBar: AppBar(
backgroundColor: Colors.indigo,
title: Text("Cart"),),
body: cart.basketItems.length==0
?Text("no items"):ListView.builder(
itemCount: cart.basketItems.length,
itemBuilder: (context,index){
return Card(
child: ListTile(
title: Text(cart.basketItems[index].title),),);}),);},);}}
cartmodel.dart
class Item {
String title;
String image;
double price;
Item({this.title, this.price,this.image});}
Cart.dart
class Cart extends ChangeNotifier {
List<Item> _items = [];
double _totalPrice = 0.0;
void add(Item item) {
_items.add(item);
_totalPrice += item.price;
notifyListeners();}
void remove(Item item) {
_totalPrice -= item.price;
_items.remove(item);
notifyListeners();}
int get count {
return _items.length;}
double get totalPrice {
return _totalPrice;}
List<Item> get basketItems {
return _items;}}
答案 0 :(得分:1)
出现此错误是因为您的add
方法表单Cart
类正在等待Item
对象,并且您传递了productname
这是一个字符串。
从您的代码中,您需要像这样在Item
小部件中构建新的Quantities
:
cart.add(Item(title: productname, image: productimage, price : productprice));
但是,如果您想改善代码,可以将Item
类上的Quantities
对象替换为这3个属性。您可以先将您的班级转换为StatelessWidget
:
class Quantities extends StatelessWidget {
final Item item;
Quantities(this.item);
@override
Widget build() {
...
通常,您应该在可能的方法/小部件上使用该Item
对象