In Flutter, you can map two lists together using the List.generate function or the Iterable.zip method from the collection package. Here's how you can do it using both methods:
List<String> keys = ['a', 'b', 'c']; List<int> values = [1, 2, 3]; Map<String, int> result = Map.fromIterables(keys, List.generate(keys.length, (index) => values[index])); print(result); // Output: {a: 1, b: 2, c: 3} First, ensure you have added the collection package to your pubspec.yaml file:
dependencies: flutter: sdk: flutter collection: ^1.15.0
Then, import the package and use Iterable.zip:
import 'package:collection/collection.dart'; List<String> keys = ['a', 'b', 'c']; List<int> values = [1, 2, 3]; Map<String, int> result = Map.fromIterables(keys, IterableZip([keys, values]).map((item) => item[1])); print(result); // Output: {a: 1, b: 2, c: 3} Both approaches will produce a map where elements of the keys list are keys, and elements of the values list are values in the resulting map. Adjust the code according to the types of your lists and the specific mapping logic you need.
Mapping Two Lists into a Dictionary in Flutter
List<String> keys = ['name', 'age', 'city']; List<dynamic> values = ['Alice', 30, 'New York']; Map<String, dynamic> dictionary = Map.fromIterables(keys, values); print(dictionary); // Output: {name: Alice, age: 30, city: New York} Combining Two Lists into a Map with Dart
zip method.List<String> keys = ['id', 'title', 'price']; List<dynamic> values = [101, 'Book', 9.99]; Map<String, dynamic> map = Map.fromIterables(keys, values); print(map); // Output: {id: 101, title: Book, price: 9.99} Creating a List of Tuples from Two Lists in Flutter
List<String> list1 = ['a', 'b', 'c']; List<int> list2 = [1, 2, 3]; List<Tuple> tuples = []; for (var i = 0; i < list1.length; i++) { tuples.add(Tuple(list1[i], list2[i])); } print(tuples); // Output: [Tuple(a, 1), Tuple(b, 2), Tuple(c, 3)] // Define the Tuple class class Tuple { final dynamic item1; final dynamic item2; Tuple(this.item1, this.item2); @override String toString() { return 'Tuple($item1, $item2)'; } } Merging Two Lists of Objects into a Map
List<String> list1 = ['x', 'y', 'z']; List<int> list2 = [10, 20, 30]; Map<String, int> map = Map.fromIterables(list1, list2); print(map); // Output: {x: 10, y: 20, z: 30} Mapping Two Lists into a Custom Data Structure in Flutter
class Person { final String name; final int age; Person(this.name, this.age); @override String toString() { return 'Person(name: $name, age: $age)'; } } List<String> names = ['Alice', 'Bob', 'Charlie']; List<int> ages = [25, 30, 35]; List<Person> people = []; for (var i = 0; i < names.length; i++) { people.add(Person(names[i], ages[i])); } print(people); // Output: [Person(name: Alice, age: 25), Person(name: Bob, age: 30), Person(name: Charlie, age: 35)] Creating a Dictionary from Two Lists with Default Values in Flutter
List<String> keys = ['name', 'age', 'country']; List<dynamic> values = ['John', 25]; // Missing one value Map<String, dynamic> map = { for (int i = 0; i < keys.length; i++) keys[i]: i < values.length ? values[i] : 'N/A' // Default value }; print(map); // Output: {name: John, age: 25, country: N/A} Mapping Two Lists into a List of Dictionaries in Flutter
List<String> keys = ['name', 'city']; List<dynamic> values = [['Alice', 'Bob'], ['New York', 'Los Angeles']]; List<Map<String, dynamic>> listOfMaps = []; for (var i = 0; i < values[0].length; i++) { Map<String, dynamic> map = { for (int j = 0; j < keys.length; j++) keys[j]: values[j][i] }; listOfMaps.add(map); } print(listOfMaps); // Output: [{name: Alice, city: New York}, {name: Bob, city: Los Angeles}] Creating a Map from Two Lists with Custom Key Transformations
List<String> keys = ['a', 'b', 'c']; List<int> values = [1, 2, 3]; // Custom key transformation Map<String, int> map = { for (var i = 0; i < keys.length; i++) keys[i].toUpperCase(): values[i] // Transforming keys to uppercase }; print(map); // Output: {A: 1, B: 2, C: 3} Mapping Two Lists into a Dictionary with Type Safety
List<String> keys = ['one', 'two', 'three']; List<int> values = [1, 2, 3]; Map<String, int> map = Map<String, int>.fromIterables(keys, values); print(map); // Output: {one: 1, two: 2, three: 3} Mapping Two Lists to Create a Dictionary with Specific Conditions
List<String> keys = ['k1', 'k2', 'k3']; List<int> values = [10, 20, 30]; Map<String, int> map = {}; for (int i = 0; i < keys.length; i++) { if (values[i] > 15) { // Only add items where the value is greater than 15 map[keys[i]] = values[i]; } } print(map); // Output: {k2: 20, k3: 30} svn-export fastcgi device-admin odata google-translate line-endings usb row-value-expression user-agent stdout