简体   繁体   English

如何处理 Flutter 谷歌地图的控制器

[英]How to dispose Flutter google map's controller

I was using google_maps_flutter library in my project.我在我的项目中使用了google_maps_flutter库。 While i hot reload the map or back to map from another view then it crashes with message :当我热重新加载地图或从另一个视图返回地图时,它会崩溃并显示消息:

Exception has occurred.
PlatformException (PlatformException(error, java.lang.IllegalStateException: Trying to create an already created platform view, view id: 0
    at io.flutter.plugin.platform.PlatformViewsController$1.createPlatformView(PlatformViewsController.java:85)
    at io.flutter.embedding.engine.systemchannels.PlatformViewsChannel$1.create(PlatformViewsChannel.java:96)

So i was trying to dispose the map and its controller.所以我试图处理地图及其控制器。 i got a code snippet to dispose in this article So i added this code snippet:在这篇文章中有一个代码片段要处理所以我添加了这个代码片段:

@override
  void dispose() {
    _disposeController();
    super.dispose();
  }

  Future<void> _disposeController() async{
    final GoogleMapController controller = await _controller.future;
    //controller.dispose();
  }

but uncommenting last line was giving this error:但取消注释最后一行给出了这个错误:

 The method 'dispose' isn't defined for the class 'GoogleMapController'.
Try correcting the name to the name of an existing method, or defining a method named 'dispose'.

then how can i dispose the controller?那我该如何处置控制器呢?

I just faced the same trouble and realized, that they drastically changed the GoogleMapController implementation between this medium article and the current version.我刚刚遇到了同样的麻烦并意识到,他们彻底改变了这篇中等文章和当前版本之间的 GoogleMapController 实现。

Also, the readme on the package could be outdated, I used the examples from the package itself:此外,包上的自述文件可能已经过时,我使用了包本身的示例:

eg https://github.com/flutter/plugins/blob/master/packages/google_maps_flutter/google_maps_flutter/example/lib/animate_camera.dart例如https://github.com/flutter/plugins/blob/master/packages/google_maps_flutter/google_maps_flutter/example/lib/animate_camera.dart

They seem to work really well.他们似乎工作得很好。

Update更新

It seems like they have gotten rid of the Completer way and in their examples they do not need this construction anymore.似乎他们已经摆脱了 Completer 方式,并且在他们的示例中,他们不再需要这种结构。

That means: Use the GoogleMapController directly without a completer:这意味着:在没有完成程序的情况下直接使用 GoogleMapController:

GoogleMapController mapController;
// instead of 
// GoogleMapController mapController;

just assign to this variable onMapCreated:只需分配给这个变量 onMapCreated:

onMapCreated: (GoogleMapController controller) {
  _controller.complete(controller);
},

You can then use this controller without awaiting the future.然后您可以使用此控制器而无需等待未来。

mapController.animateCamera(
  CameraUpdate.newLatLng(
    const LatLng(56.1725505, 10.1850512),
  ),
);

I have not seen any need to dispose this instance, it is not implemented anymore through the ChangeNotifier() class.我没有看到任何需要处理这个实例,它不再通过 ChangeNotifier() 类实现。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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