Mamikon Papikyan
Mamikon Papikyan

Reputation: 39

How to add any symbols after prefix_?

is there any solution? e.g. I have data in Map with key favorites_ prefix and values _suffix (for example: favorites_jeans, favorites_suit,...,). I want to by dint of loop get that values and set in List, because of it I must give keys of map, right? I want to know how can I get values of myMap["favorites_*"] (* - after the favorites_ any symbols).

Upvotes: 0

Views: 32

Answers (2)

Susheel Karam
Susheel Karam

Reputation: 927

As per what I understood, you want to fetch value from map using "favorites_" + a dynamic value from list as key.

You just have to use String templates and use $ to insert suffix variable to build key dynamically:

List<String> suffixList = ["jeans", "suit", "shirt"];

for(String suffix in suffixList) {
  var item = myMap["favorites_$suffix"];
  // Do something with item
}

Hope it helps

Upvotes: 1

HII
HII

Reputation: 4129

 List<String> favoritesStrings = ['favorite_name','favorite_jeans',];
  Map<String,dynamic> myMap = {
    favoritesStrings[0]:'0',
    favoritesStrings[1]:'1',
    'someKey':'2',
    'anotherKey':'3',
  };
  favoritesStrings.forEach((favorite)=>print(myMap[favorite]));//prints 0 1

Upvotes: 1

Related Questions