Arvy Larope
Arvy Larope

Reputation: 3

Adding List Data to Another List Based on Their Value

I have a List:

    List<MenuItem> makanan = [
  MenuItem(
    gambarMenu: Image.asset('images/nasigoreng.jpeg'),
    namaMenu: 'Nasi Goreng',
    priceMenu: 'Rp. 15.000',
    qty: 0,
    note: 'Catatan',
  ),
  MenuItem(
    gambarMenu: Image.asset('images/mie goreng.jpeg'),
    namaMenu: 'Mie Goreng',
    priceMenu: 'Rp. 15.000',
    qty: 0,
    note: 'Catatan',
  ),
  MenuItem(
    gambarMenu: Image.asset('images/nasigoreng.jpeg'),
    namaMenu: 'Mie Kuah',
    priceMenu: 'Rp. 15.000',
    qty: 0,
    note: 'Catatan',
  ),
  MenuItem(
    gambarMenu: Image.asset('images/nasigoreng.jpeg'),
    namaMenu: 'Nasi Campur',
    priceMenu: 'Rp. 15.000',
    qty: 0,
    note: 'Catatan',
  ),
  MenuItem(
    gambarMenu: Image.asset('images/nasigoreng.jpeg'),
    namaMenu: 'Bakso Komplit',
    priceMenu: 'Rp. 15.000',
    qty: 0,
    note: 'Catatan',
  ),
];

also have Model that I want to make list from my previous list like this:

class TableOrder {
  String namaMakanan;
  int qtyMakanan;
  String noteMakanan;

  TableOrder({this.namaMakanan, this.qtyMakanan, this.noteMakanan});
}

How can I get data from previous list into new list based on their qty value, if the qty is more than 0 that data will be added into new list.

Upvotes: 0

Views: 520

Answers (2)

Mensch
Mensch

Reputation: 700

use the where method of list

List<MenuItem> newMakanan = makanan.where((item) => item.qty > 0).toList();

docs here

Upvotes: 0

Yahya Ayash Luqman
Yahya Ayash Luqman

Reputation: 492

You can use List.where and List.map like this:

final List<TableOrder> newList = makanan
  .where((item) => item.qty > 0)
  .map((item) => TableOrder(namaMakanan: item.namaMenu, ...))
  .toList();

Upvotes: 1

Related Questions