English 中文(简体)
How can i update all item in List that is == true to a given condition
原标题:
  • 时间:2023-05-26 23:28:37
  •  标签:
  • flutter
  • dart

i have the following List

List myList = [
{ name : Alex },
{ name : Ali },
{ name : jack },
{ name : Alex },
{ name : Alex },
];

Now i want update all item that the name == Alex to Andy

so i tried the following

int wantedIndex =  myList.indexWhere((element) =>  element[ name ]== Alex );
  myList[wantedIndex] = { name : Andy } ;

since the indexWhere is only get the first target index so it will update one item only.

so How could i achieve this with all item that == Alex ?

问题回答

You could use map method.

myList.map((item) {
  if (item[ name ] ==  Alex ) item[ name ] =  Andy ;
}).toList();

print(myList); // [{name: Andy}, {name: Ali}, {name: jack}, {name: Andy}, {name: Andy}]




相关问题
Flutter App cannot be installed on Android TV

I m building a Flutter app that should support Android TV and Mobile devices. Despite Google Play shows that it is supported, I cannot install app on my MiBox device. While trying to install it, both ...

Moving the icon button to the right causes rendering issues

I am trying to align the icon button to the right of the Text field, I tried the row which made the icon button not centered vertically and now I am trying out the Stack widget. Here is my code - ...

热门标签