2019-08-13
Convert two lists in to dictionary in dart programming flutter
stackoverflow
Question

I want to convert two arrays in to a dictionary. i have two dynamic lists after that i need to combine two lists as dictionary(map)

var x = ["car", "train", "bus"];
var y = ["https://car", "https://train", "https://bus"]

Expected:
 final Map<String, String> z = ["car": "https://car", "train": "https://train", "bus": "https://bus"];
Answer
1

You could do something like this, although I'm sure there are cleaner ways out there:

var z = new Map<String, String>(); 

for( var i = 0; i <= x.length; i++ ) { 
  z[x[i]] = y[i]; 
} 

Additionally, look into the putIfAbsent function, as it may come in handy if you need to add more key/value pairs

Convert two lists in to dictionary in dart programming flutter
See more ...