从地图中删除元素

时间:2018-03-29 22:46:05

标签: dart flutter

如何在地图上进行迭代,逐个删除元素,然后在删除每个元素后调用一个函数?

假设我有发布功能

  final HashMap<int, bool> _instances = new HashMap<int, bool>();

  void release(dynamic instance) {
    if (_instances[instance.hashCode] != null) {
      _releaseHashCode(instance.hashCode);
      _dispatcher.dispatchEvent(PinEvent.release);
    }
  }

  void _releaseHashCode(int hashCode) => _instances.remove(hashCode);

现在我想创建releaseAll(),为地图中的每个元素调用_releaseHashCode,从而释放它并调度发布事件。

  void releaseAll() {
    ...
  }

2 个答案:

答案 0 :(得分:1)

选中removeWhere

Fatal Exception: java.lang.NullPointerException: Attempt to read from null array
   at java.util.ArrayList.get(ArrayList.java:310)
   at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.g.a(g.java:56)
   at com.google.maps.api.android.lib6.gmm6.vector.gl.drawable.ao.a(ao.java:91)
   at com.google.maps.api.android.lib6.gmm6.store.fetch.i.a(i.java:99)
   at com.google.maps.api.android.lib6.gmm6.store.fetch.f.a(f.java:3)
   at com.google.maps.api.android.lib6.gmm6.store.j.a(j.java:19)
   at com.google.maps.api.android.lib6.gmm6.store.k.a(k.java:96)
   at com.google.maps.api.android.lib6.gmm6.store.k.d(k.java:178)
   at com.google.maps.api.android.lib6.gmm6.store.k.a(k.java:31)
   at com.google.maps.api.android.lib6.gmm6.store.f.handleMessage(f.java:100)
   at android.os.Handler.dispatchMessage(Handler.java:111)
   at android.os.Looper.loop(Looper.java:194)
   at com.google.maps.api.android.lib6.gmm6.store.k.run(k.java:126)
   at com.google.maps.api.android.lib6.gmm6.store.ab.run(ab.java:12)

答案 1 :(得分:0)

我就这样做了

  void releaseAll() {
    var tempMap = new HashMap<int, bool>()..addAll(_instances);
    tempMap.forEach((int hashCode, bool value) {
      _releaseHashCode(hashCode);
      _dispatcher.dispatchEvent(PinEvent.release);
    });
    tempMap.clear();
    tempMap = null;
  }