7

The code is on DartPad if you need a complete example (see the while loop towards the end.)

I have a loop,

Place place = places[0];
while (places.isNotEmpty) {
  // Get a list of places within distance (we can travel to)
  List reachables = place.getReachables();

  // Get the closest reachable place
  Place closest = place.getClosest(reachables);

  // Remove the current place (ultimately should terminate the loop)
  places.remove(place);

  // Iterate
  place = closest;
}

But it's not removing place on the second-to-last line. i.e., the length of the places list remains the same, making it an infinite loop. What's wrong?

live-love
  • 48,840
  • 22
  • 240
  • 204
Ganymede
  • 3,345
  • 1
  • 22
  • 17

6 Answers6

5

This could be because the object in the list has a different hashCode from the object you are trying to remove.

Try using this code instead, to find the correct object by comparing the objects properties, before removing it:

var item = list.firstWhere((x) => x.property1== myObj.property1 && x.property2== myObj.property2, orElse: () => null);

list.remove(item);

Another option is to override the == operator and hashCode in your class.

class Class1 {
  @override
  bool operator==(other) {
    if(other is! Class1) {
      return false;
    }
    return property1 == (other as Class1).property1;
  }

  int _hashCode;
  @override
  int get hashCode {
    if(_hashCode == null) {
      _hashCode = property1.hashCode
    }
    return _hashCode;
  }
}
live-love
  • 48,840
  • 22
  • 240
  • 204
2

I have faced the very same issue. Unfortunately I haven't found the root cause, but in the same situation I replaced

places.remove[place]

with

places.removeWhere(p => p.hachCode == place.hashCode)

as a workaround. One more approach was helpful too:

// Get the place from your set:
final place = places.first;
// Replace the place in the set:
places.add(place);
// Remove the place from the set:
places.remove(place);
Kir Perepyolov
  • 251
  • 2
  • 7
0

Most likely place is not in the list for some reason. It's hard to debug without knowing the exact data used, the problem doesn't reproduce with the three-place sample you have in the linked DartPad.

Try figuring out which element is causing the problem. For example you can try adding an if (!places.contains(place)) print("!!! $place not in $places"); before the remove, or something similar that detects the state when the problem occurs.

lrn
  • 64,680
  • 7
  • 105
  • 121
  • The `place = places[0]; while(...) { places.remove(place); }` [error] is confounding to me. And nowhere else am I removing from the `places` list. I'm sure it's a bug in my code, I'll keep hacking on it. – Ganymede Dec 18 '15 at 10:32
  • you can try this answer :) https://stackoverflow.com/a/70569910/12121284 – Ankit Parmar Jan 03 '22 at 18:24
0

This way you can remove object from dynamic list

List data = [
    {
      "name":"stack"
    },
    {
      "name":"overflow"
    }
  ];
  
  data.removeWhere((item) => item["name"]=="stack");
  
  print(data);

Output

[{name: overflow}]
Ankit Parmar
  • 449
  • 5
  • 8
0

Use the plugin Equatable

class Place extends Equatable { ... }

https://pub.dev/packages/equatable

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 19 '22 at 06:38
0

I was having the same issue. I did something like this using removeWhere.

myList.removeWhere((item) => item.id == yourItemId.id)
Syed Arsalan Kazmi
  • 949
  • 1
  • 11
  • 17