我正在运行list.firstWhere
,这有时会引发异常:
Bad State: No element
When the exception was thrown, this was the stack:
#0 _ListBase&Object&ListMixin.firstWhere (dart:collection/list.dart:148:5)
我不明白这意味着什么也无法通过查找at the source来识别问题。
我的firstWhere
看起来像这样:
list.firstWhere((element) => a == b);
当没有匹配元素时会发生这种情况,即当a == b
对于list
中的任何元素都不为真时,并且未指定可选参数orElse
。
您还可以指定orElse
来处理这种情况:
list.firstWhere((element) => a == b, orElse: () => print('No matching element.'));