简体   繁体   中英

The expression has a type of 'void' so its value can't be used - Flutter Dart unable to add custom object to list in for loop

I need to create a new list 'filteredSlots' which will have the slots from 'slotlist' list which has its availabilty and its consecutive slots availability is also set to true.

But I get an error for add(). Please help me resolve this

class Slot {
  String time;
  bool isAvailable;

  Slot({this.time, this.isAvailable});
}

class FilterSlots {
  List<Slot> slotlist = [
    Slot(time: '10:00', isAvailable: true),
    Slot(time: '10:30', isAvailable: false),
    Slot(time: '11:00', isAvailable: true),
    Slot(time: '11:30', isAvailable: true),
    Slot(time: '12:00', isAvailable: true),
    Slot(time: '12:30', isAvailable: false),
    Slot(time: '13:00', isAvailable: true),
  ];

  List<Slot> filteredSlots = [];

  List<Slot> filterSlots() {
    for (var i = 0; i < slotlist.length - 1; i++) {
      if ((slotlist[i].isAvailable) & (slotlist[i + 1].isAvailable)) {
        filteredSlots = filteredSlots.add(slotlist[i]);
      }
    }
    return filteredSlots;
  }
}

//Expected output,

List<Slot> filteredslots = [
  Slot(time: '11:00', isAvailable: true),
  Slot(time: '11:30', isAvailable: true),
];

make bool isAvailable; a static variable static bool isAvailable;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM