【Dart/Flutter】Map型変数の値をfor in loopで取得する良い方法

Map型変数の値をfor in loopで取得する良い方法を紹介します。

Map型変数の値をfor in loopで取得する時、下記のコードをよく見かけます。

Map<String, String> map = {
'key1': 'value1',
'key2': 'value2',
};

for (var key in map.keys) {
print(map[key]);
}

しかし、公式サイトにもっとかっこいい書き方が紹介されています。MapEntryを使う方法です。

for (var MapEntry(key: key, value: value) in map.entries) {
print('key:$key value:$value');
}

シンプルにこのようにも書けます。

for (var MapEntry(: key, : value) in map.entries) {
print('key:$key value:$value');
}

ご参考までに。

公式サイト

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

keisuke ishikura
keisuke ishikura

Written by keisuke ishikura

Mypace Co.,Ltd. CEO/ Mobile Engineer

No responses yet

What are your thoughts?