リストから where
で部分集合を得た後、 .indexOf
で要素の位置を取得したかったが、次のようなエラーが出た。
final sentencesInThisSection = allSentences
.where((sentence) => sentence['section'] == thisSectionNo)
final sentencePositionInThisSection =
sentencesInThisSection.indexOf(thisSentence);
════════ (2) Exception caught by widgets library ═══════════════════════════════════════════════════
Class 'WhereIterable<dynamic>' has no instance method 'indexOf'.
Receiver: Instance of 'WhereIterable<dynamic>'
Tried calling: indexOf(_LinkedHashMap len:5)
where
で取り出した部分集合はList
でなく、WhereIterable
という型らしい。
次のようにtoList
でList
に変換することで、indexOf
が使用可能になる。
final sentencesInThisSection = allSentences
.where((sentence) => sentence['section'] == thisSectionNo)
.toList();
final sentencePositionInThisSection =
sentencesInThisSection.indexOf(thisSentence);