Unhandled Exception: Bad state: Cannot add new events after calling close, on using fluttter_google_places

302

This is a plugin error which they have fixed in their git code. Unfortunately it hasn't been updated yet on their current release.

What you can do is integrate all of the src files in their github lib folder. It resolved the issue for me.

The exact steps:

[1] Go to https://github.com/fluttercommunity/flutter_google_places/tree/master/lib/src [2] Copied all the dart files into my project

Share:
302
Hassan Hammad
Author by

Hassan Hammad

Updated on November 21, 2022

Comments

  • Hassan Hammad
    Hassan Hammad over 1 year

    I have been getting this error every time i use the PlacesAutoComplete widget. Search/request/response everything is working perfectly, no crashes or anything. But the logs show me this error on every search.

    I added two packages for it: flutter_google_places and google_web_services.

    And i dont use streams at all. So this is a really strange error for me.

    I followed a couple of online guides for this, can anyone explain what am i doing wrong?

    Also, i see that this is a pretty basic way to implement places autocomplete. I would love your suggestions on a better way to implement google places search.

    Here is the error i get

    Future<void> _searchWithPlacesAutoComplete() async {
        Prediction _predictions = await PlacesAutocomplete.show(
          context: context,
          apiKey: GOOGLE_API_KEY,
          onError: (response) => _showError(response),
          mode: Mode.overlay,
          language: "en",
          components: [Component(Component.country, "uk")],
          location: _currentLocation,
          radius: 50,
        );
    
        if (_predictions != null) {
          PlacesDetailsResponse detail =
              await GoogleMapsPlaces(apiKey: GOOGLE_API_KEY)
                  .getDetailsByPlaceId(_predictions.placeId);
          final lat = detail.result.geometry.location.lat;
          final lng = detail.result.geometry.location.lng;
    
          setState(() {
            _markedPosition = new LatLng(lat, lng);
          });
          mapController.animateCamera(
            CameraUpdate.newCameraPosition(
              CameraPosition(
                target: _markedPosition,
                zoom: 16.0,
              ),
            ),
          );
        }
      }