Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Google Maps StateError (Bad state: Future already completed)

I'm trying to display Google maps inside a dialog box and for the first time it pops up as expected, however the second time it throws and exception: StateError (Bad state: Future already completed).

Completer<GoogleMapController> _controller = Completer();
_displayDialog(){
  Alert(
    context: context,
    style: alertStyle,
    title: "Here are your results:",
    content: Column(
      children: <Widget>[
        Container(
          width: 200.0,
          height: 200.0,
          child: GoogleMap(
            //mapType: MapType.hybrid,
            initialCameraPosition: _kGooglePlex,
            onMapCreated: (GoogleMapController controller) {
              _controller.complete(controller); //throws here in this line
            },
          ),
        ),      
      ],
    ),

Here is a gif to summarize whats happening

Im using rflutter_alert: ^1.0.3 for the dialog box, and google_maps_flutter: ^0.5.21+15 for Maps.

Thanks in advance!

like image 556
lorincm Avatar asked Dec 11 '22 01:12

lorincm


1 Answers

this verifies if previously I already called "completer ()" if so, you don't need to call "completer ()" again, and just skip it in your code

onMapCreated: (GoogleMapController controller) {
    if (!_controller.isCompleted) {
       //first calling is false
       //call "completer()"
      _controller.complete(controller);
    }else{
       //other calling, later is true, 
      //don't call again completer()
    }
}
like image 173
chico rockmantico Avatar answered Jun 07 '23 02:06

chico rockmantico