CallMe
CallMe

Reputation: 13

Map for Multi List entry

I have dynamic list have multi value as dynamic number needs to convert to string like below example. when I test it in dartpad it will work only if I have one entry in (listN) but when I have multi entry in (listN) will show me null please advice

void main() { 
      Map <dynamic,String> month= {
        1: 'January',
        2: 'February',
        3: 'March',
        4: 'April', 
        5: 'May', 
        6: 'June', 
        7: 'July', 
        8: 'August', 
        9: 'September', 
        10: 'October', 
        11: 'November', 
        12: 'December',
  };
  
  List<dynamic> listN = [1,3,4];
  
  print (month[listN]);  
}

I expect show me the results as (January March April)

Upvotes: 1

Views: 62

Answers (1)

Ravindra S. Patil
Ravindra S. Patil

Reputation: 14865

Try below code:

void main() {
  Map<dynamic, String> month = {
    1: 'January',
    2: 'February',
    3: 'March',
    4: 'April',
    5: 'May',
    6: 'June',
    7: 'July',
    8: 'August',
    9: 'September',
    10: 'October',
    11: 'November',
    12: 'December',
  };
  List<int> listN = [1, 3, 4];

  List<String> selectedMonths = listN.map((key) => month[key]!).toList();

  print(selectedMonths);
}

Output:

[January, March, April]

Upvotes: 0

Related Questions