简体   繁体   中英

how to use classes in flutter/dart

i have created a class of products. 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':

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 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'; 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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