Lists, Maps, and Sets help you store and organize groups of data in your app. They make it easy to keep track of many items at once.
Lists, Maps, and Sets in Flutter
Introduction
Syntax
Flutter
List<String> fruits = ['apple', 'banana', 'orange']; Map<String, int> scores = {'Alice': 90, 'Bob': 85}; Set<String> uniqueNames = {'Alice', 'Bob', 'Alice'};
Lists keep items in order and allow duplicates.
Maps store pairs of keys and values for quick lookup.
Sets store unique items without order.
Examples
Flutter
List<int> numbers = []; // An empty list with no items
Flutter
Map<String, String> capitals = {'USA': 'Washington', 'France': 'Paris'}; // Map with country names as keys and capitals as values
Flutter
Set<String> letters = {'a', 'b', 'c', 'a'}; // Set will only keep unique letters: 'a', 'b', 'c'
Sample App
This Flutter app shows how to use a List, a Map, and a Set. It displays the fruits list, the scores map, and the unique names set on the screen.
Flutter
import 'package:flutter/material.dart'; void main() { runApp(const MyApp()); } class MyApp extends StatelessWidget { const MyApp({super.key}); @override Widget build(BuildContext context) { List<String> fruits = ['Apple', 'Banana', 'Orange']; Map<String, int> scores = {'Alice': 90, 'Bob': 85}; Set<String> uniqueNames = {'Alice', 'Bob', 'Alice'}; return MaterialApp( home: Scaffold( appBar: AppBar(title: const Text('Lists, Maps, and Sets')), body: Padding( padding: const EdgeInsets.all(16), child: Column( crossAxisAlignment: CrossAxisAlignment.start, children: [ Text('List of fruits: ' + fruits.join(', ')), const SizedBox(height: 10), Text('Scores Map:'), for (var entry in scores.entries) Text('${entry.key}: ${entry.value}'), const SizedBox(height: 10), Text('Unique names in Set: ' + uniqueNames.join(', ')), ], ), ), ), ); } }
Important Notes
Lists keep the order of items and allow duplicates.
Maps let you find values quickly using keys.
Sets automatically remove duplicate items.
Use Lists when order matters, Maps when you want key-value pairs, and Sets when you want unique items.
Summary
Lists store ordered items and allow duplicates.
Maps store key-value pairs for quick lookup.
Sets store unique items without order.
Practice
1. Which of the following correctly describes a
Set in Flutter?easy
Solution
Step 1: Understand Set characteristics
A Set stores unique items and does not maintain order.Step 2: Compare with other collections
Maps store key-value pairs, Lists allow duplicates and are ordered.Final Answer:
A collection of unique items without any specific order. -> Option DQuick Check:
Set = unique unordered collection [OK]
Hint: Remember: Sets have unique items, no order [OK]
Common Mistakes:
- Confusing Sets with Lists (which allow duplicates)
- Thinking Sets maintain insertion order
- Mixing up Maps and Sets
2. Which of the following is the correct syntax to create a Map in Flutter?
easy
Solution
Step 1: Identify Map syntax
Maps use curly braces with key-value pairs separated by colons.Step 2: Check each option
var myMap = {'key1': 'value1', 'key2': 'value2'}; correctly uses {'key': 'value'} pairs. Others are list, set, or invalid syntax.Final Answer:
var myMap = {'key1': 'value1', 'key2': 'value2'}; -> Option AQuick Check:
Map syntax = {'key': 'value'} [OK]
Hint: Maps use curly braces with colon-separated pairs [OK]
Common Mistakes:
- Using commas instead of colons for key-value pairs
- Confusing list syntax with Map syntax
- Trying to create Map with Set syntax
3. What will be the output of this Flutter code?
var list = [1, 2, 3, 2]; var set = list.toSet(); print(set);
medium
Solution
Step 1: Convert list to set
CallingtoSet()removes duplicates and creates a Set.Step 2: Understand Set output
Set prints with curly braces and unique values only: {1, 2, 3}.Final Answer:
{1, 2, 3} -> Option AQuick Check:
List to Set removes duplicates [OK]
Hint: toSet() removes duplicates and changes brackets [OK]
Common Mistakes:
- Expecting list brackets [] instead of set braces {}
- Thinking duplicates remain after toSet()
- Confusing output format of Set vs List
4. Identify the error in this Flutter code snippet:
var map = {'a': 1, 'b': 2};
map.add('c', 3);medium
Solution
Step 1: Check Map methods
Maps do not have an add() method; use map[key] = value to add entries.Step 2: Validate other options
Keys can be strings, Maps can have many pairs, and declaration syntax is correct.Final Answer:
The method add() does not exist for Map. -> Option BQuick Check:
Use map[key] = value to add entries [OK]
Hint: Use map[key] = value to add, not add() method [OK]
Common Mistakes:
- Trying to use add() on Map instead of assignment
- Thinking Map keys must be integers
- Assuming Map size is limited
5. Given this Flutter code, what will be the final content of
result?
var list = [1, 2, 2, 3];
var map = {for (var i in list) i: i * 2};
var result = map.values.toSet().toList();
print(result);hard
Solution
Step 1: Create Map from list
Map comprehension uses list items as keys and doubles as values. Duplicate keys overwrite, so map = {1:2, 2:4, 3:6}.Step 2: Extract values and remove duplicates
map.values = [2, 4, 6]. Converting to Set removes duplicates (none here), then toList() keeps order.Final Answer:
[2, 4, 6] -> Option CQuick Check:
Map keys unique, values doubled, duplicates removed [OK]
Hint: Map keys unique; duplicates overwrite values [OK]
Common Mistakes:
- Expecting duplicate keys to keep all entries
- Confusing keys and values in map comprehension
- Not converting Set back to List
Recommended
Ready to prove it?
Pass the test and this pattern moves unknown
10 questions ~5 mineasy hard
