find a class object from List<Object> by property by value not by index and update another property in that object

I have a list in which objects gets added, modified and removed very frequently with asynchronous calls (Dart).

I don't want to update object by index to ensure that the target object is only modified.

I tried this

class Foo(){
    prop1;
    prop2;
}

class Bar(){
    List<Foo> list = List();

    Bar(){
        this.list = [foo1, foo2, foo3,];
    }

    updateList(someObj) async {
        this.list.asMap().entries.forEach(entry){
            if (entry.value.prop1 == someObj.prop1){
                this.list[entry.key].prop2 = someObj.prop2  
                // update object prop by index is working as of now, but 
               // multiple update calls can lead to update of wrong object 
            }
        }
    }
    
    addObj(someObj) async {
       ...
    }
    
    deleteObj(someObj) async {
       ...
    }
}

// calling function (async or without async) 
doSomething(){ 
   barObj.updateList(someObj); // this is a async function
   otherFunc(); // this line is not blocked
}

Can any one suggest me better way to update object in a list ensuring same object is updated?

Upvotes: 0

Views: 34

Answers (0)

Related Questions