17
7

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 3 years have passed since last update.

[dart] mapでもindexがほしい - asMapは便利 -

Last updated at Posted at 2020-03-19

この記事は

配列をmapで展開したときに、本来は拾ってこれない要素番号(index)をどうにか取得しようとした結果、foreachとasMap()を使えば取得できました。という記事です。

コード

List _sample = ['a','b','c'];

   _sample.asMap().forEach(
    (index, value) => print("$index : $value")
  );

asMap()について

リストをマップ化できます。
_sample = ['a','b','c']asMap()することで、{0: a, 1: b, 2: c}に変化します。

mapの代わりにforEachを使う

indexを取得するためにmapの代わりにforEachを使います。

参考

Flutter DartでIndex付きのmapを簡単に行うExtension
Enumerate or map through a list with index and value in Dart

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?