List firstWhere Bad state: No element

1818 views dart
5

I am running list.firstWhere and this is sometimes throwing an exception:

Bad State: No element

When the exception was thrown, this was the stack:
#0      _ListBase&Object&ListMixin.firstWhere (dart:collection/list.dart:148:5)

I do not understand what this means an also could not identify the issue by looking at the source.

My firstWhere looks like this:

list.firstWhere((element) => a == b);

answered question

1 Answer

1

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((element) => a == b, orElse: () => print('No matching element.'));

posted this

Have an answer?

JD

Please login first before posting an answer.