Home > database >  how to use classes in flutter/dart
how to use classes in flutter/dart

Time:08-24

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.

CodePudding user response:

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

CodePudding user response:

A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Dart gives built-in support for this concept called class.

Visit this Page: https://flutterbyexample.com/lesson/classes

  • Related