在 dart 编程 flutter 中将两个列表转换为字典

yug*_*g k 5 dart flutter

我想将两个数组转换为字典。我有两个动态列表,之后我需要将两个列表合并为字典(地图)

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"];
Run Code Online (Sandbox Code Playgroud)

Jas*_*son 2

你可以做这样的事情,尽管我确信有更干净的方法:

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

for( var i = 0; i <= x.length; i++ ) { 
  z[x[i]] = y[i]; 
} 
Run Code Online (Sandbox Code Playgroud)

此外,请查看putIfAbsent函数,因为如果您需要添加更多键/值对,它可能会派上用场