Home > Net >  How to get one item from a list flutter
How to get one item from a list flutter

Time:01-04

I have a product details page that fetches product details based on the data from the product class. But I'm confused about how to display the details page based on a particular product id.

Here is my product model class

class Product {
  int id;
  String name;
  String description;
  String shortDescription;
  String sku;
  String price;
  String regularPrice;
  String salePrice;
  String stockStatus;
  List<Images> images;
  List<Categories> categories;
  List<Attributes> attributes;
  List<int> relatedIds;
  String type;
  VariableProduct variableProduct;



  Product({
    this.id,
    this.name,
    this.description,
    this.shortDescription,
    this.sku,
    this.price,
    this.regularPrice,
    this.salePrice,
    this.stockStatus,
    this.attributes,
    this.relatedIds,
    this.type,
    this.variableProduct
  });

  Product.fromJson(Map<String, dynamic> json) {
    id = json['id'];
    name = json['name'];
    description = json['description'];
    shortDescription = json['short_description'];
    sku = json['sku'];
    price = json['price'];
    regularPrice = json['regular_price'];
    salePrice = json['sale_price'] != "" ? json['sale_price'] : json['regular_price'];
    stockStatus = json['stock_status'];
    relatedIds = json["cross_sell_ids"].cast<int>();
    type = json['type'];
    if (json['categories'] != null) {
      categories = List<Categories>();
      json['categories'].forEach((v) {
        categories.add(Categories.fromJson(v));
      });
    }
    if (json['images'] != null) {
      images = <Images>[];
      json['images'].forEach((v) {
        images.add(Images.fromJson(v));
      }); 
    }

    if (json['attributes'] != null) {
      attributes = List<Attributes>();
      json['attributes'].forEach((v) {
        attributes.add(Attributes.fromJson(v));
      });
    }
  }
}

In my product detail page, I use final Product data; to access the product details data. How can I do this based on the product id as seen in the model class... I hope my question is understandable

CodePudding user response:

If your products are containing in a List, you can try this:

final product = products[1];

In this example you get a product from the list of products by 1 index (not position).

List in Dart also have methods for taking first and last elements from the list without giving an index:

final first = products.first;
final last = products.last;

CodePudding user response:

You can use firstWhere to find product by id.

 int searchProductId = 3; // your product id

 Product it = items.firstWhere((p) => p.id == searchProductId);

More about List.

Also, you can pass the selected Product though details page constructor.

CodePudding user response:

Iterables have a method firstWhere.

Let's say you have a products list and you want to find a product having id = 10

Product p = products.firstWhere((product) => product.id == 10);

You get the product having id = 10 in p

You will get an error if no such element exists in the list. In that case, there is a parameter orElse which you can use to return a default value in case the element is not found. Check the documentation linked above.

  • Related