简体   繁体   中英

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!

I think that the problem is because you are trying to complete a Completer twice which is not allowed. What I did bellow was to create a new Completer each time you call _displayDialog() .

_displayDialog(){
  Completer<GoogleMapController> _controller = Completer();

  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
            },
          ),
        ),      
      ],
    ),

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()
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM