LoginSignup
0
0

More than 3 years have passed since last update.

[Dart]map は動かないが、for in だと動く場合に確認すべきこと

Posted at

結論

mapでtoListを呼び忘れている。

説明

Dartがmapで返すものはIterableであり、これは即座に実行結果を返すものではない。

final list = ['hoge', 'fuga', 'piyo'];
list.map(print); // Iterableを返却しているだけで、printは実行されない。

toList()が実行されたときに、渡してきた関数が実行される。

final list = ['hoge', 'fuga', 'piyo'];
list.map(print).toList(); // toList()が呼ばれたときにmapで渡したprintが実行される。

// 上と下は同じ結果になる。

for (var el in list) {
  print(el);
}

終わりに

Dart使いはじめたときに、map動かないのに、for in動くなーとか思っていたらこれだったので書きました。
TS書いたあとに、Dart書くと忘れてやらかす。

参考

0
0
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
0
0