【问题标题】:Flutter: Google Maps StateError (Bad state: Future already completed)颤振:谷歌地图状态错误(坏状态:未来已经完成)
【发布时间】:2020-04-18 12:19:29
【问题描述】:

我正在尝试在对话框中显示 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 用于地图。

提前致谢!

【问题讨论】:

    标签: google-maps exception flutter dart


    【解决方案1】:

    我认为问题在于您尝试完成两次Completer,这是不允许的。我在下面所做的是每次调用_displayDialog() 时创建一个新的Completer

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

    【讨论】:

      【解决方案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()
          }
      }
      

      【讨论】:

      • 你拯救了我的一天,兄弟
      猜你喜欢
      • 2021-05-05
      • 2018-12-26
      • 1970-01-01
      • 2019-06-24
      • 1970-01-01
      • 1970-01-01
      • 2020-05-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多