尝试从列表中删除对象时在迭代过程中进行并发修改

时间:2019-01-11 16:34:36

标签: dart flutter

我试图循环多个列表,最后比较它们的名称,如果它们不匹配,则将其从列表中删除...

但出现此错误:

  

在迭代过程中并发修改:

尽管我已经复制了原始列表,只是为了避免此错误..但仍在获取它..我尝试的是:

globals.filteredPollsList = pollsList;
pollsList.forEach( (p) {
  for (var e in entitiesList) {
    print(e.name);
    for (var entity in entitiesFilter) {
      print(entity);
      print(e.name);
      if (e.name != entity && p.entityId != e.id) {
        print('true');
        globals.filteredPollsList.remove(p);
      }
    }
  }
});

但出现此错误:

flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
flutter: The following ConcurrentModificationError was thrown while handling a gesture:
flutter: Concurrent modification during iteration: Instance(length:8) of '_GrowableList'.
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #0      List.forEach (dart:core/runtime/libgrowable_array.dart:279:36)
flutter: #1      _ShapedWidgetState.filterPoll (package:moi_polls_app/pages/home.page.dart:1484:15)
flutter: #2      _ShapedWidgetState.build.<anonymous closure>.<anonymous closure>.<anonymous closure> (package:moi_polls_app/pages/home.page.dart:1896:45)
flutter: #3      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
flutter: #4      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
flutter: #5      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
flutter: #6      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
flutter: #7      TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:175:7)
flutter: #8      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:315:9)
flutter: #9      PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:73:12)
flutter: #10     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:101:11)
flutter: #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:180:19)
flutter: #12     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:158:22)
flutter: #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:138:7)
flutter: #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:101:7)
flutter: #15     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:85:7)
flutter: #16     _invoke1 (dart:ui/hooks.dart:168:13)
flutter: #17     _dispatchPointerDataPacket (dart:ui/hooks.dart:122:5)
flutter:
flutter: Handler: onTap
flutter: Recognizer:
flutter:   TapGestureRecognizer#e99b3(debugOwner: GestureDetector, state: possible, won arena, finalPosition:
flutter:   Offset(283.3, 608.7), sent tap down)

如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

如果您要复制列表,请使用以下方法:

globals.filteredPollsList = List.from(pollsList);