0

I want to get availableSlots = allSlots-bookedSlots and here is the code, Please help!

final List<timeSlot> allSlots=
[
TimeSlot(time: '6PM-7PM', isSelected: false),
TimeSlot(time: '7PM-8PM', isSelected: false),
TimeSlot(time: '8PM-9PM', isSelected: false),
];
final List<timeSlot> bookedSlots=
[
TimeSlot(time: '6PM-7PM', isSelected: false),
TimeSlot(time: '7PM-8PM', isSelected: false),
];

here is the TimeSlot class

  class TimeSlot {
  String time;
  bool isSelected;

  TimeSlot({this.isSelected, this.time});
}

2 Answers 2

1

At first override '==' operator to compare two TimeSlot values.

And filter bookedSlots item from allSlots to get availableSlots.

enter image description here

void main() {
  final List<TimeSlot> allSlots=
  [
    TimeSlot(time: '6PM-7PM', isSelected: false),
    TimeSlot(time: '7PM-8PM', isSelected: false),
    TimeSlot(time: '8PM-9PM', isSelected: false),
  ];
  final List<TimeSlot> bookedSlots=
  [
    TimeSlot(time: '6PM-7PM', isSelected: false),
    TimeSlot(time: '7PM-8PM', isSelected: false),
  ];
  
  
  List<TimeSlot> availableSlots = allSlots.where((item) => !bookedSlots.contains(item)).toList();
  print(allSlots);
  print(availableSlots);
}

 class TimeSlot {
  String? time;
  bool? isSelected;

  TimeSlot({this.isSelected, this.time});
   
  @override
  bool operator==(Object other) {
    return other is TimeSlot && other.time == time &&other.isSelected == isSelected;
  }
   
   
   @override
  String toString() => '''TimeSlot {
     time: $time,
     isSelected: $isSelected,
  }''';
   

   @override
   int get hashCode => Object.hash(time, isSelected);
   
}


Sign up to request clarification or add additional context in comments.

5 Comments

@AdilNaseem I changed what you want like that do not change original list.
It shows the wrong result. Is there any other solution? Please help.
I print two lists that allSlots and availableSlots. The availableSlots has only one item TimeSlot { time: 8PM-9PM, isSelected: false, }.
if I change number of items in both list, it shows wrong result. I appreciate your help. you helped a lot. I request Just a little bit more. Please
Would you update that case at question body? Or if you can explain by comment, let me know that.
1

Copy the code below into the dartpad if you wanted to test: https://dartpad.dev/


void main() {
  
  
  List<SomeClass> list1 = [SomeClass('item 1'), SomeClass('item 2'), SomeClass('item 3')];
  List<SomeClass> list2 = [SomeClass('item 1'), SomeClass('item 3')];
  

  List<SomeClass> deltaList = [];
   
  print(list1);
  
  deltaList = list1;
  
  print(deltaList);
  
  for(SomeClass item in list2){
   
    deltaList.removeWhere((delta) => delta.name == item.name);
      
  }
  
  for (SomeClass item in deltaList){
    print(item.name);
  }
  
 
}


class SomeClass{
  String name;
  SomeClass(this.name);
}

6 Comments

this only works for built-in datatypes. It does not works in objects
@AdilNaseem Yes you're right! I think this may work, although this might be a bit clunky
Why is it modifying the original list1?
@AdilNaseem It isn't modifying the list1, that's the purpose of the new list 'deltaList'. deltaList is essentially the availableSlots.
Ok but I dont want to remove items from list1.
|

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.