Home > Blockchain >  Checking and deleting elements from a set of list in dart
Checking and deleting elements from a set of list in dart

Time:11-19

I am new to dart so sorry if I'm missing something obvious I'm writing a code to add elements to this Set<List> which works fine, but when I try to check if the element exists or to delete it does not work.

here's the code

void main(List<String> args) {
  Set<List<double>> level1barriers = {};
  print(level1barriers);
  level1barriers.add([1, 1]);
  level1barriers.add([0.5, 0.3]);
  print(level1barriers);
  print(level1barriers.contains([1, 1]));
  level1barriers.remove([1, 1]);
  print(level1barriers);
}

CodePudding user response:

I used a variable instead and it worked

void main(List<String> args) {
  Set<List<double>> level1barriers = {};
  print(level1barriers);

  List<double> list = [1,1];

  level1barriers.add(list);
  level1barriers.add([0.5, 0.3]);
  print(level1barriers);
  print(level1barriers.contains(list));
  level1barriers.remove(list);
  print(level1barriers);
}

Console :

{}
{[1, 1], [0.5, 0.3]}
true
{[0.5, 0.3]}

CodePudding user response:

Contains checks if one item is identically to another, so if you use the same exact object it will return true but if only the values is the same it wont.

list1 = [1,1]
list2 = [1,1]
list1 == list2 -> Returns false.

So, you have to options if you want a Set, one is instead of making a Set<List> make a Set and make that class have as an only object a List. And make it Equalable.

  • Related