How can I find a list knowing is name in the form of String?

Solution for How can I find a list knowing is name in the form of String?
is Given Below:

I have a lot of lists List<String> each one in a dart file. Every list contains the name of cities in various countries.

After choosing a country, I’m able to have the name of the list I want, for instance “list_cities_japan”, as a String.

How can I a choose the good list to show the cities with this String?

The best solution would be to map your countries to the different Lists. Here is what that solution would look like in one file:

Map<String, List<String>> cities = {
    "list_cities_japan" : ["tokoyo", "Osaka", "kobe"], 
    "list_cities_india" : ["Delhi", "manglore", "kerela"]
}; 
  
  //prints complete map
  print(cities); 
  
  //print list of countries : (list_cities_japan, list_cities_india)
  print(cities.keys); 
  
  //prints cities in country list: [tokoyo, Osaka, kobe]
  print(cities["list_cities_japan"]); 

Solution across multiple files:

  1. File one

     List<String> list_cities_japan = ["tokoyo", "Osaka", "kobe"];
    
  2. File two

     List<String> list_cities_india = ["Delhi", "manglore", "kerela"];
    
  3. Main file

     import 'fileone'; 
     import 'filetwo'; 
    
     void main() {
       Map<String, List<String>> cities = {
       "list_cities_japan" : list_cities_japan, 
       "list_cities_india" : list_cities_india
       }; 
    
       //print list of countries :(list_cities_japan,list_cities_india)
       print(cities.keys); 
    
       //prints cities in country list: [tokoyo, Osaka, kobe]
       print(cities["list_cities_japan"]); 
     }
    

Map all the countries to it’s respective list of cities, something like:

  var map = [
    {
      'country': 'list_cities_japan',
      'cities': ['Hiroshima', 'Nagasaki'],
    }
  ];

And from there, you can access every list of cities of a given country