0

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.

2
  • why dont you use List.where? Commented Jun 1, 2020 at 3:24
  • hi @DelphiX, I am new to Dart/Flutter, can you elaborate? Commented Jun 1, 2020 at 3:26

2 Answers 2

1

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();
Sign up to request clarification or add additional context in comments.

1 Comment

this works even better, because using another model for new list instead of filtering the current model, I can able to reset the makanan list to zero without affecting the new list.
0

use the where method of list

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

docs here

1 Comment

thank you DelphiX, this works. but I changed a little bit List<MenuItem> newMakanan = makanan.where((i) => i.qty > 0).toList();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.