简体   繁体   English

如何在颤振/飞镖中使用类

[英]how to use classes in flutter/dart

i have created a class of products.我创建了一个 class 产品。 then i created an empty list of products named cart.然后我创建了一个名为购物车的空产品列表。 then i am trying to add products to this cart and printing it, it is showing instance of product on console.然后我尝试将产品添加到此购物车并打印它,它在控制台上显示产品实例。

class product  {
  late String name;
  late int id,size,price;


  product(String name,int id,int size,int price){
    this.name=name;
    this.size=size;
    this.id=id;
    this.price=price;
  }
}

List cart=[];列出购物车=[];

wishlist page:愿望清单页面:

class wishlist extends StatefulWidget {
  const wishlist({Key? key}) : super(key: key);
  @override
  State<wishlist> createState() => _wishlistState();
}
class _wishlistState extends State<wishlist> {

  late String category;
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: BackButton(
          color: Colors.black,
        ),
        backgroundColor: Color(0xFF45F1B9),
        title: Center(
          child: Text('Categories',
            style: TextStyle(
              color: Colors.black
            ),
          ),
        ),
      ),
      backgroundColor: Colors.white,
      body: ListView.builder(
        itemCount: categories.length,
        itemBuilder:(BuildContext context,int index){
          return Card(child: ListTile(title: Text(categories[index].name,
             ),
            onTap: (){
              Navigator.push(
                context,
                MaterialPageRoute(builder: (context) => cpproducts1(categories[index].prod),),
              );
            },
            ),
          );
        },
      ),
    );
  }
}

second page'cproducts':第二页'cproducts':

class cpproducts1 extends StatelessWidget {
 late List<product> prod;
   cpproducts1( this.prod, {Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView.builder(
        itemCount:prod.length ,
        itemBuilder:(BuildContext context,int index){
          return Card(child: ListTile(title: Text(prod[index].name,),
            trailing: Text('\$${prod[index].price}'),
            onTap: (){
              cart.add(prod[index]);
              print(cart);
            },
          ),
          );
        },
      ),
    );
  }
}

cart.add(prod[index]);
print(cart);

after these line run it says instance of product.在这些行运行之后,它会显示产品实例。

i need the products to be added to the cart list so i can use them我需要将产品添加到购物车列表中,以便我可以使用它们

does anybody know the answer to this.有没有人知道这个问题的答案。

You can create a constructor in a much Simpler way, and to know the actual value rather than Instance of Class you have to add override toString method.您可以以更简单的方式创建构造函数,并且要知道实际值而不是 Class 的实例,您必须添加覆盖toString方法。

class Product  {
  const Product(this.name, this.id, this.size, this.price);

  late final String name;
  late final int id,size,price;

 @override
 String toString() => '$name, $id, $size, $price'; 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM