firstWhere
retuns the newList which generated based on the condition
void main() {
List<String> list = ['red', 'yellow', 'pink', 'blue'];
var newList = list.firstWhere((element) => element == 'green',
orElse: () => 'No matching color found');
print(newList);
}
Output:
No matching color found
OR
void main() {
List<String> list = ['red', 'yellow', 'pink', 'blue'];
var newList = list.firstWhere((element) => element == 'blue',
orElse: () => 'No matching color found');
print(newList);
}
Output:
blue
If orElse not defined in the code and the wrong item gets search which doesn't exist in the list then it shows BadStateException
void main() {
List<String> list = ['red', 'yellow', 'pink', 'blue'];
var newList = list.firstWhere((element) => element == 'green');
print(newList);
}
Output:
Uncaught Error: Bad state: No element