Skip to content

[google_maps_flutter_web] Bad state: Cannot add new events after calling close #168782

Closed
flutter/packages
#9250

Description

@JamesMcIntosh

PR

flutter/packages#9250

Related issue tickets

#159388
#75818

Steps to reproduce

While the map is animating to the new position dispose the map (i.e. replace it in the widget tree).
Inspect the developer tools console for the error message.

Expected results

No error messages in the console as the disposed widget should no longer be listening to map events.

Actual results

Error messages in the console as events are still trying to be sent from the map to the controller.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:google_maps_flutter/google_maps_flutter.dart';

void main() {
  runApp(const MapToggleWidget());
}

class MapToggleWidget extends StatefulWidget {
  const MapToggleWidget({Key? key}) : super(key: key);

  @override
  _MapToggleWidgetState createState() => _MapToggleWidgetState();
}

class _MapToggleWidgetState extends State<MapToggleWidget> {
  bool _showMap = true;
  GoogleMapController? _mapController;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: const Text('Map Toggle Example')),
        body: Column(
          children: [
            Expanded(child: _showMap ? buildGoogleMap() : buildNoMapContainer()),
            Padding(
              padding: const EdgeInsets.all(8.0),
              child: ElevatedButton(
                onPressed: () => setState(() => _showMap = !_showMap),
                child: Text(_showMap ? 'Hide Map' : 'Show Map'),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Container buildNoMapContainer() {
    return Container(
      color: Colors.grey[300],
      child: const Center(
        child: Text(
          'Map is hidden',
          style: TextStyle(fontSize: 18),
        ),
      ),
    );
  }

  GoogleMap buildGoogleMap() {
    return GoogleMap(
      mapType: MapType.satellite,
      initialCameraPosition: const CameraPosition(
        target: LatLng(-38.9000, 175.8000),
        zoom: 10.0,
      ),
      onMapCreated: _onMapCreated,
    );
  }

  void _onMapCreated(GoogleMapController controller) {
    _mapController = controller;
    // wait a moment for layout, then move camera to fit the bounds
    Future.delayed(const Duration(milliseconds: 100), () {
      _mapController?.animateCamera(
        CameraUpdate.newLatLngBounds(
            LatLngBounds(
              southwest: const LatLng(-38.9000, 175.8000),
              northeast: const LatLng(-38.4000, 176.4000),
            ),
            50.0),
        duration: const Duration(seconds: 5),
      );
    });
  }
}

Screenshots or Video

No response

Logs

Logs
errors.dart:307 Uncaught (in promise) DartError: Bad state: Cannot add new events after calling close
    at Object.throw_ [as throw] (errors.dart:307:3)
    at async._AsyncBroadcastStreamController.new.add (broadcast_stream_controller.dart:256:24)
    at google_maps_controller.dart:271:7
    at _RootZone.runUnaryGuarded (zone.dart:1778:9)
    at [_sendData] (stream_impl.dart:381:5)
    at async._DelayedData.new.perform (stream_impl.dart:573:13)
    at async._PendingEvents.new.handleNext (stream_impl.dart:678:10)
    at async._AsyncCallbackEntry.new.callback (stream_impl.dart:649:7)
    at Object._microtaskLoop (schedule_microtask.dart:40:11)
    at _startMicrotaskLoop (schedule_microtask.dart:49:5)
    at async_patch.dart:186:7

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.2, on macOS 15.4.1 24E263 darwin-arm64, locale en-AU) [2.1s]
    • Flutter version 3.29.2 on channel stable at /Users/james/fvm/versions/3.29.2
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c236373904 (9 weeks ago), 2025-03-13 16:17:06 -0400
    • Engine revision 18b71d647a
    • Dart version 3.7.2
    • DevTools version 2.42.3.

[✓] Chrome - develop for the web [31ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

Activity

  1. danagbemava-nc commented on May 14, 2025

    @danagbemava-nc
    Member

    Hi @JamesMcIntosh, what device(s) are you seeing this issue on? I can't seem to reproduce the issue using the code sample and steps outlined above. I tested on a Pixel 7 and an iPhone 16 Pro Max

  2. added
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 14, 2025
  3. JamesMcIntosh commented on May 14, 2025

    @JamesMcIntosh
    Author

    Hi @danagbemava-nc
    This issue is for the google maps web package (google_maps_flutter_web), you need to be testing it in a web browser i.e. Chrome

  4. removed
    waiting for responseThe Flutter team cannot make further progress on this issue until the original reporter responds
    on May 14, 2025
  5. danagbemava-nc commented on May 15, 2025

    @danagbemava-nc
    Member

    I somehow missed that this was web-specific (my bad). Reproducible using the code sample and steps outlined above.

    flutter doctor -v
    [✓] Flutter (Channel stable, 3.29.3, on macOS 15.4 24E248 darwin-arm64, locale en-US) [1,830ms]
        • Flutter version 3.29.3 on channel stable at /Users/deanli/dev/stable
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision ea121f8859 (5 weeks ago), 2025-04-11 19:10:07 +0000
        • Engine revision cf56914b32
        • Dart version 3.7.2
        • DevTools version 2.42.3
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [4.3s]
        • Android SDK at /Users/deanli/Library/Android/sdk
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents/jbr/Contents/Home/bin/java
          This JDK is specified in your Flutter configuration.
          To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.4) [2.0s]
        • Xcode at /Applications/Xcode-16.4.0-Beta.app/Contents/Developer
        • Build 16F1t
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [31ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [30ms]
        • Android Studio at /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • android-studio-dir = /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    
    [✓] Android Studio (version 2024.2) [27ms]
        • Android Studio at /Users/deanli/Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)
    
    [✓] VS Code (version 1.99.3) [26ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (6 available) [8.3s]
        • Nexus (wireless) (mobile)       • 00008020-001875E83A38002E            • ios            • iOS 18.4.1 22E252
        • iPhone (wireless) (mobile)      • 00008030-0019281E01F8802E            • ios            • iOS 18.4.1 22E252
        • iPhone 16 Pro (mobile)          • 63C28242-27C4-477D-A63A-BC88BADB1BCA • ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-5 (simulator)
        • macOS (desktop)                 • macos                                • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Mac Designed for iPad (desktop) • mac-designed-for-ipad                • darwin         • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)                    • chrome                               • web-javascript • Google Chrome 136.0.7103.94
        ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for iPhone 16 Pro Max. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
    
    [✓] Network resources [1,613ms]
        • All expected network resources are available.
    
    • No issues found!
    
    [!] Flutter (Channel master, 3.33.0-1.0.pre.85, on macOS 15.4 24E248 darwin-arm64, locale en-US) [2.6s]
        • Flutter version 3.33.0-1.0.pre.85 on channel master at /Users/deanli/dev/master
        ! Warning: `flutter` on your path resolves to /Users/deanli/dev/stable/bin/flutter, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/master. Consider adding /Users/deanli/dev/master/bin to the front of your path.
        ! Warning: `dart` on your path resolves to /Users/deanli/dev/stable/bin/dart, which is not inside your current Flutter SDK checkout at /Users/deanli/dev/master. Consider adding /Users/deanli/dev/master/bin to the front of your path.
        • Upstream repository https://github.com/flutter/flutter.git
        • Framework revision 3cb2f5f6a5 (79 minutes ago), 2025-05-15 07:17:23 +0200
        • Engine revision 3cb2f5f6a5
        • Dart version 3.9.0 (build 3.9.0-117.0.dev)
        • DevTools version 2.46.0
        • If those were intentional, you can disregard the above warnings; however it is recommended to use "git" directly to perform update checks and upgrades.
    
    [✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [2.2s]
        • Android SDK at /Users/deanli/Library/Android/sdk
        • Emulator version 35.5.9.0 (build_id 13248646) (CL:N/A)
        • Platform android-36, build-tools 36.0.0
        • Java binary at: /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents/jbr/Contents/Home/bin/java
          This JDK is specified in your Flutter configuration.
          To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
        • All Android licenses accepted.
    
    [✓] Xcode - develop for iOS and macOS (Xcode 16.4) [1,621ms]
        • Xcode at /Applications/Xcode-16.4.0-Beta.app/Contents/Developer
        • Build 16F1t
        • CocoaPods version 1.16.2
    
    [✓] Chrome - develop for the web [14ms]
        • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome
    
    [✓] Android Studio (version 2024.3) [12ms]
        • Android Studio at /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • android-studio-dir = /Users/deanli/Applications/Android Studio Meerkat Feature Drop 2024.3.2 RC 4.app
        • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)
    
    [✓] Android Studio (version 2024.2) [10ms]
        • Android Studio at /Users/deanli/Applications/Android Studio.app/Contents
        • Flutter plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/9212-flutter
        • Dart plugin can be installed from:
          🔨 https://plugins.jetbrains.com/plugin/6351-dart
        • Java version OpenJDK Runtime Environment (build 21.0.5+-12932927-b750.29)
    
    [✓] VS Code (version 1.99.3) [9ms]
        • VS Code at /Applications/Visual Studio Code.app/Contents
        • Flutter extension version 3.110.0
    
    [✓] Connected device (5 available) [6.9s]
        • Nexus (wireless) (mobile)  • 00008020-001875E83A38002E            • ios            • iOS 18.4.1 22E252
        • iPhone (wireless) (mobile) • 00008030-0019281E01F8802E            • ios            • iOS 18.4.1 22E252
        • iPhone 16 Pro (mobile)     • 63C28242-27C4-477D-A63A-BC88BADB1BCA • ios            • com.apple.CoreSimulator.SimRuntime.iOS-18-5 (simulator)
        • macOS (desktop)            • macos                                • darwin-arm64   • macOS 15.4 24E248 darwin-arm64
        • Chrome (web)               • chrome                               • web-javascript • Google Chrome 136.0.7103.94
        ! Error: Browsing on the local area network for Dean’s iPad. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
        ! Error: Browsing on the local area network for iPhone 16 Pro Max. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
          The device must be opted into Developer Mode to connect wirelessly. (code -27)
    
    [✓] Network resources [786ms]
        • All expected network resources are available.
    
    ! Doctor found issues in 1 category.
    
  6. added
    p: mapsGoogle Maps plugin
    packageflutter/packages repository. See also p: labels.
    has reproducible stepsThe issue has been confirmed reproducible and is ready to work on
    fyi-ecosystemFor the attention of Ecosystem team
    team-webOwned by Web platform team
    and removed
    in triagePresently being triaged by the triage team
    on May 15, 2025
  7. JamesMcIntosh commented on May 15, 2025

    @JamesMcIntosh
    Author

    Thanks @danagbemava-nc for testing it out!
    There is another PR I submitted which is complementary to this issue but in the parent package google_maps_flutter flutter/packages#9227
    I have added a similar example app to it (just without the animated camera reposition) to facilitate reproducing the issue.

  8. added
    platform-webWeb applications specifically
    P2Important issues not at the top of the work list
    triaged-webTriaged by Web platform team
    on May 21, 2025
  9. marked Changing the theme #172108 as a duplicate of this issue on Jul 18, 2025
  10. added
    r: fixedIssue is closed as already fixed in a newer version
    on Sep 4, 2025
  11. github-actions commented on Sep 18, 2025

    @github-actions

    This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

  12. locked as resolved and limited conversation to collaborators on Sep 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P2Important issues not at the top of the work listfound in release: 3.29Found to occur in 3.29found in release: 3.33Found to occur in 3.33has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: mapsGoogle Maps pluginpackageflutter/packages repository. See also p: labels.platform-webWeb applications specificallyr: fixedIssue is closed as already fixed in a newer versionteam-webOwned by Web platform teamtriaged-webTriaged by Web platform team

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions