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.
0
0
Lists, Maps, and Sets in Flutter
Introduction
When you want to keep a list of tasks or names in order.
When you need to connect keys to values, like a phone book with names and numbers.
When you want to store unique items without duplicates, like a set of favorite colors.
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
This is an empty list. You can add items later.
Flutter
List<int> numbers = []; // An empty list with no items
Maps connect keys to values. Here, countries to their capitals.
Flutter
Map<String, String> capitals = {'USA': 'Washington', 'France': 'Paris'}; // Map with country names as keys and capitals as values
Sets remove duplicates automatically.
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(', ')), ], ), ), ), ); } }
OutputSuccess
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.