List firstWhere Bad state: No element - dart

 This will happen when there is no matching element, i.e. when a == b is never true for any of the elements in list and the optional parameter orElse is not specified.

You can also specify orElse to handle the situation:


list.firstWhere((a) => a == b, orElse: () => print('No matching element.'));


If you want to return null instead when there is no match, you can also do that with orElse:

list.firstWhere((a) => a == b, orElse: () => null);

Post a Comment

Thank you

Previous Post Next Post