【问题标题】:Remove list of Objects from other list of objects in flutter从颤动的其他对象列表中删除对象列表
【发布时间】:2022-01-16 21:28:37
【问题描述】:

我想获得 availableSlots = allSlots-bookedSlots,这是代码,请帮忙!

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),
];

这是 TimeSlot 类

  class TimeSlot {
  String time;
  bool isSelected;

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

【问题讨论】:

    标签: list flutter dart


    【解决方案1】:

    首先覆盖“==”运算符以比较两个 TimeSlot 值。

    并从 allSlots 中过滤 bookingSlots 项目以获取 availableSlots。

    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);
       
    }
    
    
    

    【讨论】:

    • @AdilNaseem 我改变了你想要的,不要改变原始列表。
    • 显示错误的结果。还有其他解决方案吗?请帮忙。
    • 我打印两个列表,即 allSlots 和 availableSlots。 availableSlots 只有一项 TimeSlot { time: 8PM-9PM, isSelected: false, }.
    • 如果我更改了两个列表中的项目数,它会显示错误的结果。我感谢您的帮助。你帮了很多忙。我要求再多一点。请
    • 您会在问题正文中更新该案例吗?或者,如果您可以通过评论进行解释,请告诉我。
    【解决方案2】:

    如果您想测试,请将以下代码复制到 dartpad 中: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);
    }
    

    【讨论】:

    • 这仅适用于内置数据类型。它不适用于对象
    • @AdilNaseem 是的,你是对的!我认为这可能有效,尽管这可能有点笨拙
    • 为什么要修改原来的list1?
    • @AdilNaseem 它不是在修改 list1,这是新列表“deltaList”的目的。 deltaList本质上是availableSlots。
    • 好的,但我不想从 list1 中删除项目。
    猜你喜欢
    • 2014-06-18
    • 2020-06-04
    • 2021-08-03
    • 2021-11-24
    • 1970-01-01
    • 2011-10-22
    • 1970-01-01
    • 2022-01-04
    相关资源
    最近更新 更多