Learn/Dart for Flutter/Lists & Maps

Lists & Maps

Work with collections of data

5 min~4 min read+30 FX

You can read this lesson publicly. Sign in to unlock exercises, quiz, and FX progress tracking.

Table of Contents

  1. 1. Common List Operations
  2. 2. Choosing Between List, Map, and Model Objects
  3. 3. This Lesson In Simple Words
  4. 4. Your level focus
  5. 5. One practical habit
  6. 6. Deep Understanding Guide
  7. 7. Why this lesson matters
  8. 8. Study flow for better retention

Having trouble? AI is here to help

Ask context-aware questions about this lesson. Free plan: 5 questions/day. Upgrade to Premium for higher daily limits.

Use the AI button at the bottom of the screen

Lists in Dart

Lists are ordered collections of items, similar to arrays in other programming languages. They are one of the most commonly used data structures in Dart and Flutter.

dart
1// Creating lists
2var fruits = ['apple', 'banana', 'orange'];
3List<String> vegetables = ['carrot', 'broccoli'];
4
5// Accessing elements
6print(fruits[0]); // apple
7print(fruits.first); // apple
8print(fruits.last); // orange
9
10// Modifying
11fruits.add('mango');
12fruits.remove('banana');
13fruits.insert(1, 'grape');
14
15// List properties
16print(fruits.length); // 4
17print(fruits.isEmpty); // false

Common List Operations

dart
1var numbers = [1, 2, 3, 4, 5];
2
3// map - transform each element
4var doubled = numbers.map((n) => n * 2).toList();
5// [2, 4, 6, 8, 10]
6
7// where - filter elements
8var evens = numbers.where((n) => n % 2 == 0).toList();
9// [2, 4]
10
11// any / every - check conditions
12numbers.any((n) => n > 3); // true
13numbers.every((n) => n > 0); // true
14
15// Spread operator
16var more = [0, ...numbers, 6]; // [0, 1, 2, 3, 4, 5, 6]

Maps in Dart

Maps store data as key-value pairs, similar to dictionaries in Python or objects in JavaScript. They are useful for representing structured data like user profiles or API responses.

dart
1// Creating maps
2var user = {
3 'name': 'John',
4 'age': 25,
5 'email': 'john@example.com'
6};
7
8Map<String, dynamic> product = {
9 'id': 1,
10 'title': 'Phone',
11 'price': 999.99
12};
13
14// Accessing values
15print(user['name']); // John
16
17// Adding/updating
18user['role'] = 'admin'; // Add new
19user['age'] = 26; // Update
20
21// Checking keys
22user.containsKey('name'); // true
23user.keys; // (name, age, email, role)
24user.values; // (John, 26, john@example.com, admin)

Maps in Flutter

💡 Maps in Flutter

JSON from APIs becomes `Map<String, dynamic>`. You will use maps constantly when working with data.

Choosing Between List, Map, and Model Objects

Use the right structure for the job:

  • List: ordered items (chat messages, products, tasks)
  • Map: key-value data (JSON payloads, lookup tables)
  • Model class: typed app domain objects (User, Product, Order)

In production apps, raw map usage should be short-lived. Convert map data into typed model classes quickly to reduce runtime errors and improve autocomplete support.

Collection Safety Checklist

  • Check for empty list before reading first or last item.
  • Avoid direct dynamic casting when typed parsing is possible.
  • Use map/where intentionally and convert toList when needed.
  • Normalize API maps before they reach the UI layer.

This Lesson In Simple Words

Work with collections of data. Dart is the language behind Flutter, and clear Dart code makes your Flutter UI easier to maintain.

Your level focus

Build confidence with basics first; speed comes later.

One practical habit

Prefer readable names, explicit intent, and async/await over deeply nested callbacks.

Deep Understanding Guide

Why this lesson matters

When Dart basics are weak, state bugs, null crashes, and async confusion increase quickly.

Study flow for better retention

  1. Read one section, run one tiny example, then explain the idea in your own words.
  2. Treat each Dart concept as a building block for widgets, services, and tests you will write later.
  3. If you can teach it in 45 seconds without notes, you understood it.

Common Mistakes To Avoid

  • Ignoring null safety and forcing values with ! without checking data flow.
  • Writing long functions that mix validation, transformation, and side effects together.
  • Using dynamic where a proper type would catch mistakes earlier.
  • Catching broad errors without keeping useful context for debugging.

Practice Sprint (15-25 Minutes)

  • Before running sample code, predict output and behavior in one sentence.
  • Write a small pure function and test it with edge cases.
  • Convert one callback-based snippet to async/await with proper error handling.
  • Refactor a long function into smaller helper methods with clear names.
  • Complete the required exercises without using hints on the first attempt.
  • Answer this question out loud before checking quiz options: "What is printed?".

Code Comprehension Drill

  • State the purpose of the code block in one line before reading details.
  • Mark inputs, state changes, and final output.
  • Change one small value and predict what should happen before running.
  • Run again and compare your prediction with actual behavior.

Lesson Completion Checklist

  • I can explain "Lists & Maps" in simple words without reading notes.
  • I can describe one real app situation where this concept is required.
  • I know at least one mistake to avoid: Ignoring null safety and forcing values with ! without checking data flow.
  • I can modify one code sample and correctly predict the behavior change.
  • I can finish required exercises with understanding, not trial-and-error guesses.

Frequently Asked Questions

What is printed?

add() adds one element to the list, making length 4.

What is the result?

map() transforms each element. n * 2 doubles each number.

How do you access a map value?

Use square brackets with the key to access map values.

What does the spread operator (...) do with lists?

Spread (...) unpacks all elements of a list into another list.

Suggest Improvement

Something seems incorrect, unclear, or missing? Let us know.

About The Author

FlutterCraft editorial lessons are reviewed by experienced Flutter developers and updated to stay aligned with modern Flutter and Dart best practices.

Reference docs: Flutter Documentation, Dart Language Tour, and official package documentation where relevant.

Sign in to continue with exercises and quiz for this lesson.

Sign In to Continue