本文介绍了飞镖聚合物-打破异常:迭代期间并发修改:"ObservableList"的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个错误:"ConcurrentModificationError"-异常中断:迭代期间的并发修改:'ObservableList'的实例.

I have an error: "ConcurrentModificationError" - Breaking on exception: Concurrent modification during iteration: Instance of 'ObservableList'.

当我想在用于搜索"项目的"ForEach"期间通过ObservableList删除某些项目时.

When I want delete some items by ObservableList during "ForEach" used to "search" items.

代码示例:

.html

<template repeat="{{f in SelectedFiles}}">
    <li>
        <span class="label">{{f.name}}"">

        <button class="tiny" on-click="{{deleteDataSetFile}}" data-file="{{f.path}}">Delete</button>
    </li>
</template>

.dart

@observable List<String> fileSelected;

void deleteDataSetFile(Event event, var detail, var target) {

    String datafile = target.attributes['data-file'];

    for(var file in this.SelectedFiles){
        if(file.path==datafile){
        this.SelectedFiles.remove(file);
        }
    }
}

推荐答案

此功能不适用于大多数编程语言.

This doesn't work in most programming languages.

解决方法:

迭代列表的副本

@observable List<String> fileSelected;

void deleteDataSetFile(Event event, var detail, var target) {
  String datafile = target.attributes['data-file'];
  for(var file in this.SelectedFiles.toList()){ // added .toList() which returns a copy
    if(file.path==datafile){
      this.SelectedFiles.remove(file);
    }
  }
}

存储元素并在以后删除它们

store the elements and remove them later

@observable List<String> fileSelected;

void deleteDataSetFile(Event event, var detail, var target) {
  String datafile = target.attributes['data-file'];
  List<String> itemsToRemove = [];
  for(var file in this.SelectedFiles){ 
    if(file.path==datafile){
      itemsToRemove.add(file);
    }
  }
  for(var file in itemsToRemove) {
    this.SelectedFiles.remove(file);
  }
}

如果您进行不同的迭代,则可以在迭代时删除

if you iterate differently you can delete while iterating

  for(int i = 0; i < this.SelectedFiles.length;){ 
    if(file.path==datafile){
      this.SelectedFiles.removeAt(i)
    } else {
      i++;
    }
  }

或使用 List

this.SelectedFiles.retainWhere((e) => e.path != dataFile);
this.SelectedFiles.removeWhere((e) => e.path == dataFile);

这篇关于飞镖聚合物-打破异常:迭代期间并发修改:"ObservableList"的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:45