'Unhandled Exception: Concurrent modification during iteration: Instance(length:2) of '_GrowableList'

I am using Geofire in firebase to get the location of two different users. As long as I have just 1 user it works fine but when there are more than two users online then I get the above error

 Future<void> initGeoFireListener() async {
    

    var driverLocation =
        FirebaseDatabase.instance.ref().child("availableDrivers").path;

    try {
      Geofire.initialize(driverLocation).catchError((onError) {
        print("Driver $onError");
      });
      Geofire.queryAtLocation(
              position.latitude, position.longitude, 50)! //10 km
          .listen((map) {
        print("drivers map: $map");
        if (map != null) {
          print("Nearby Drivers: $map");
          var callBack = map['callBack'];

          switch (callBack) {
            case Geofire.onKeyEntered:
              NearByAvailableDrivers nearByAvailableDrivers =
                  NearByAvailableDrivers(
                      map['key'], map['latitude'], map['longitude']);

              GeoFireAssistant.nearByAvailableDriversList
                  .add(nearByAvailableDrivers);

              updateAvailableDriversOnMap();
              // }
              break;

            case Geofire.onKeyExited: //when any driver is offline

              GeoFireAssistant.removeDriverFromList(map['key']);
              updateAvailableDriversOnMap();
              //  } else {
              print("xxxx onKeyExited ${availableDrivers.length}");
              // }
              break;

            case Geofire.onKeyMoved: //as driver position change
              NearByAvailableDrivers nearByAvailableDrivers =
                  NearByAvailableDrivers(
                      map['key'], map['latitude'], map['longitude']);

              GeoFireAssistant.updateDriverNearByLocation(
                  nearByAvailableDrivers);
              updateAvailableDriversOnMap();

              break;

            case Geofire.onGeoQueryReady:
              updateAvailableDriversOnMap();
              // } else {
              print("xxxx onGeoqueryready ${availableDrivers.length}");
              // }

              break;
          }
        } else {
          print("Drivers Null");
        }

        //  setState(() {});
      }).onError((error) {
        print("Drivers error $error");
      });
    } on PlatformException {
      print("Drivers : No platformException response");
    }
  }
void updateAvailableDriversOnMap() async {
    for (NearByAvailableDrivers driver
        in GeoFireAssistant.nearByAvailableDriversList) {  driverKey = driver.key; } //error triggered here
class NearByAvailableDrivers {
  String? key;
  double? latitude;
  double? longitude;

  NearByAvailableDrivers(this.key, this.latitude, this.longitude);
}
class GeoFireAssistant {
  static List<NearByAvailableDrivers> nearByAvailableDriversList = []; //error

  static void removeDriverFromList(String? key) {
    int index =
        nearByAvailableDriversList.indexWhere((element) => element.key == key);
    nearByAvailableDriversList.removeAt(index);
  }

  static void updateDriverNearByLocation(NearByAvailableDrivers driver) {
    int index = nearByAvailableDriversList
        .indexWhere((element) => element.key == driver.key);
    nearByAvailableDriversList[index].latitude = driver.latitude;
    nearByAvailableDriversList[index].longitude = driver.longitude;
  }
}

The error

[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: Concurrent modification during iteration: Instance(length:2) of '_GrowableList'

I do understand I am using For loop in NearByAvailableDrivers and modifying it at the same time which could be the source of the error but I am unable to fix it.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source