I am trying to add a list of items to another list of item. Both list are of different types. The items are instance of 2 different classes (String , Group). Where Group contains a list of String (members) and a method to add members to the Group. here is the code...
void main(){
void addMembersToGroups(List<String> members, List<Group> groups){
groups.forEach((group){
group.addMembers(members);
});
}
var _members=['a','b'];
var _groupsA=[Group()..addMembers(_members),Group()];
var _groupsB=[Group(members:_members),Group()];
//this function works fine
addMembersToGroups(_members,_groupsA);
//this function does not
//Throws Unhandled exception:
//Concurrent modification during iteration: Instance(length:2) of '_GrowableList'
addMembersToGroups(_members,_groupsB);
}
and the Group class
class Group{
List<String> members;
Group({this.members}){members??=[];}
void addMembers(List<String> mems) {
members.addAll(mems);
}
}
The problem is maybe in the _groupB variable where Group is initialized with the members field. Throws Unhandled exception: Concurrent modification during iteration: Instance(length:2) of '_GrowableList'
A bit explanation would be helpful. Thank you.