Home > other >  google_maps_flutter plugin not working on Android shows Error Bad state: Future already completed
google_maps_flutter plugin not working on Android shows Error Bad state: Future already completed

Time:11-23

I am using the google_maps_flutter plugin to use google maps, I followed their instructions of setting up the minSdkVersion and including the API key in AndroidManifest.xml file. I am using their example code to test the plugin but I keep getting these errors in my debug console

E/flutter (16699): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: Future already completed
E/flutter (16699): #0      _AsyncCompleter.complete (dart:async/future_impl.dart:35:31)
E/flutter (16699): #1      _GoogleMapState.onPlatformViewCreated
package:google_maps_flutter/src/google_map.dart:413
E/flutter (16699): <asynchronous suspension>
E/flutter (16699):

main.dart (I got it from the plugin page)

import 'dart:async';

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

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Google Maps Demo',
      home: MapSample(),
    );
  }
}

class MapSample extends StatefulWidget {
  @override
  State<MapSample> createState() => MapSampleState();
}

class MapSampleState extends State<MapSample> {
  Completer<GoogleMapController> _controller = Completer();

  static final CameraPosition _kGooglePlex = CameraPosition(
    target: LatLng(37.42796133580664, -122.085749655962),
    zoom: 14.4746,
  );

  static final CameraPosition _kLake = CameraPosition(
      bearing: 192.8334901395799,
      target: LatLng(37.43296265331129, -122.08832357078792),
      tilt: 59.440717697143555,
      zoom: 19.151926040649414);

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: GoogleMap(
        mapType: MapType.hybrid,
        initialCameraPosition: _kGooglePlex,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),
      floatingActionButton: FloatingActionButton.extended(
        onPressed: _goToTheLake,
        label: Text('To the lake!'),
        icon: Icon(Icons.directions_boat),
      ),
    );
  }

  Future<void> _goToTheLake() async {
    final GoogleMapController controller = await _controller.future;
    controller.animateCamera(CameraUpdate.newCameraPosition(_kLake));
  }
}

I did some debugging and found that

GoogleMap(
        mapType: MapType.hybrid,
        initialCameraPosition: _kGooglePlex,
        onMapCreated: (GoogleMapController controller) {
          _controller.complete(controller);
        },
      ),

in my main.dart file is causing the error, when I remove it I get no errors but no map as well.

In addition when I follow the link given to me in my debug console

E/flutter (16699): #1      _GoogleMapState.onPlatformViewCreated
package:google_maps_flutter/src/google_map.dart:413

It shows me this function which I think is the root problem causing the future error

Future<void> onPlatformViewCreated(int id) async {
    final GoogleMapController controller = await GoogleMapController.init(
      id,
      widget.initialCameraPosition,
      this,
    );
    _controller.complete(controller);
    _updateTileOverlays();
    final MapCreatedCallback? onMapCreated = widget.onMapCreated;
    if (onMapCreated != null) {
      onMapCreated(controller);
    }
  }

And highlights line 413 which is

_controller.complete(controller);

CodePudding user response:

Try to declare the GoogleMapController as a late variable in your MapSampleState class instead of using the Completer:

late GoogleMapController _controller;

And update the onMapCreated function:

onMapCreated: (GoogleMapController controller) {
  _controller = controller;
},

I don't know what is wrong with your code but it works for me this way (Flutter 3.3.8, google_maps_flutter 2.2.1).

If it works, make sure you don't try to use the controller before it is created or you will get a late initalization error. Alternatively, you can declare _controller as nullable, and check it is null before trying to use it:

GoogleMapController? _controller;
  • Related