Flutter:Google Maps StateError(错误状态:Future已完成)

时间:2019-12-29 11:59:49

标签: google-maps exception flutter dart

我正在尝试在对话框中显示Google地图,这是它第一次按预期方式弹出,但是第二次它抛出并抛出异常:StateError(状态错误:不良状态已经完成)。

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

我在对话框中使用 rflutter_alert:^ 1.0.3 , 和 google_maps_flutter:地图的^ 0.5.21 + 15

谢谢!

2 个答案:

答案 0 :(得分:2)

这可以验证以前是否已经调用过“ completer()”,您无需再次调用“ completer()”,只需在代码中跳过它即可。

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

答案 1 :(得分:0)

我认为问题是因为您试图完成Completer两次,这是不允许的。我所做的是每次您呼叫Completer时创建一个新的_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
            },
          ),
        ),      
      ],
    ),