Ask Your Question
0

How can I include a product in a list using Flutter?

asked 2023-05-25 10:14:56 +0000

lakamha gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-05-25 10:30:02 +0000

devzero gravatar image

Here's an example code for including a product in a list using Flutter:

class Product {
  final String name;
  final double price;

  Product(this.name, this.price);
}

class ProductList extends StatelessWidget {
  final List<Product> products = [
    Product('Apple', 1.0),
    Product('Banana', 0.5),
    Product('Orange', 0.8)
  ];

  @override
  Widget build(BuildContext context) {
    return ListView.builder(
        itemCount: products.length,
        itemBuilder: (BuildContext context, int index) {
          return ListTile(
            title: Text(products[index].name),
            trailing: Text('\$${products[index].price.toString()}'),
          );
        });
  }
}

In the code above, we define a Product class with name and price properties. We then create a ProductList class with a products list that contains three Product objects.

In the build method, we use a ListView.builder widget to build a list of ListTile widgets, with each tile displaying the name and price of a Product. We use the itemCount property of the ListView.builder to set the number of items in the list, and the itemBuilder property to define how each item should be built.

Ultimately, this results in a list of products being displayed in a Flutter app.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-25 10:14:56 +0000

Seen: 19 times

Last updated: May 25 '23