5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Dart] whereで取り出した部分集合でindexOfが使えないときの対処方法

Posted at

リストから 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 という型らしい。
次のようにtoListListに変換することで、indexOfが使用可能になる。

final sentencesInThisSection = allSentences
        .where((sentence) => sentence['section'] == thisSectionNo)
        .toList();
final sentencePositionInThisSection =
        sentencesInThisSection.indexOf(thisSentence);
5
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?