📚 Data Structures in Dart – With Examples, Uses & Practice Problems
🔍 Introduction
Data Structures are the foundation of programming and problem-solving. Dart, being a modern language used in Flutter and backend apps, supports all key data structures you need to build efficient software.
Whether you're building a quiz app, a shopping cart, or a chat system, using the right data structure can:
-
Save memory 🧠
-
Improve performance ⚡
-
Make your code easy to maintain 💡
📦 List of Common Data Structures in Dart
Data Structure | Type | Example Use Case |
---|---|---|
List |
Linear | Storing names, items, scores |
Set |
Unordered | Unique tags, categories |
Map |
Key-Value Pair | Storing user info, product prices |
Queue |
FIFO | Task scheduling, printer queue |
Stack |
LIFO | Undo/redo, browser history |
LinkedList |
Linked Nodes | Custom memory-efficient storage |
PriorityQueue |
Ordered Queue | Job scheduling, AI search |
Let’s explore them one by one with examples 👇
1️⃣ List (Ordered Collection)
void main() { List<String> names = ['Aman', 'Riya', 'Rohan']; names.add('Nita'); print(names); // Output: [Aman, Riya, Rohan, Nita]}
List<String> names = ['Aman', 'Riya', 'Rohan']; names.add('Nita'); print(names); // Output: [Aman, Riya, Rohan, Nita]}
Use cases:
-
Scores in a game
-
User comments
-
Student names
2️⃣ Set (Unordered and Unique)
void main() { Set<int> numbers = {1, 2, 3, 3}; print(numbers); // Output: {1, 2, 3}}
Set<int> numbers = {1, 2, 3, 3}; print(numbers); // Output: {1, 2, 3}}
Use cases:
-
Removing duplicates
-
Tags, filters
-
Fast lookup of unique items
3️⃣ Map (Key-Value Pairs)
void main() { Map<String, int> marks = {'Math': 90, 'Science': 80}; print(marks['Math']); // Output: 90}
Map<String, int> marks = {'Math': 90, 'Science': 80}; print(marks['Math']); // Output: 90}
Use cases:
-
User profile (name, age, email)
-
Product (name → price)
-
Lookup tables
4️⃣ Queue (First In, First Out)
import 'dart:collection';void main() { Queue<String> queue = Queue(); queue.add('Task1'); queue.add('Task2'); print(queue.removeFirst()); // Task1}
void main() { Queue<String> queue = Queue(); queue.add('Task1'); queue.add('Task2'); print(queue.removeFirst()); // Task1}
Use cases:
-
Task management
-
Event queues
-
Messaging systems
5️⃣ Stack (Last In, First Out)
void main() { List<String> stack = []; stack.add('Page1'); stack.add('Page2'); print(stack.removeLast()); // Page2}
List<String> stack = []; stack.add('Page1'); stack.add('Page2'); print(stack.removeLast()); // Page2}
Use cases:
-
Undo/redo
-
Backtracking
-
Reversing things
6️⃣ LinkedList (Doubly Linked)
import 'dart:collection';void main() { final list = LinkedList<Entry>(); list.addAll([Entry('A'), Entry('B')]); list.forEach((e) => print(e.name));}class Entry extends LinkedListEntry<Entry> { String name; Entry(this.name);}
void main() { final list = LinkedList<Entry>(); list.addAll([Entry('A'), Entry('B')]); list.forEach((e) => print(e.name));}class Entry extends LinkedListEntry<Entry> { String name; Entry(this.name);}
Use cases:
-
Custom memory-efficient collections
-
Insertion-heavy operations
7️⃣ PriorityQueue (From collection
package)
import 'package:collection/collection.dart';void main() { var queue = PriorityQueue<int>((a, b) => b.compareTo(a)); queue.addAll([5, 1, 10]); print(queue.removeFirst()); // 10}
void main() { var queue = PriorityQueue<int>((a, b) => b.compareTo(a)); queue.addAll([5, 1, 10]); print(queue.removeFirst()); // 10}
Use cases:
-
Scheduling algorithms
-
Game AI (pathfinding)
-
Huffman encoding
🌟 Why Learn These in Dart?
-
📱 Used in Flutter apps (e.g., list of widgets, user inputs)
-
💾 Backend apps (chat queue, login sessions, product catalogs)
-
🧠 Core for DSA Interviews and Competitive Coding
-
🔁 Help in managing real-time state & data flow
📱 Used in Flutter apps (e.g., list of widgets, user inputs)
💾 Backend apps (chat queue, login sessions, product catalogs)
🧠 Core for DSA Interviews and Competitive Coding
🔁 Help in managing real-time state & data flow
❓ Unsolved Problems (Practice Time!)
-
Create a List
to store daily temperatures and find the average.
-
Take a list of numbers and remove duplicates using Set
.
-
Build a Map
to store student names and their marks, then print the topper.
-
Simulate a queue of people entering a bus using Queue
.
-
Implement a calculator that uses a Stack
to reverse a string.
-
Create a to-do list manager using a PriorityQueue
(task with higher priority first).
-
Create your own linked list of names using LinkedList
.
Create a List
to store daily temperatures and find the average.
Take a list of numbers and remove duplicates using Set
.
Build a Map
to store student names and their marks, then print the topper.
Simulate a queue of people entering a bus using Queue
.
Implement a calculator that uses a Stack
to reverse a string.
Create a to-do list manager using a PriorityQueue
(task with higher priority first).
Create your own linked list of names using LinkedList
.
🧾 Summary
Structure | Ordered | Unique | Fast Lookup | Best For |
---|---|---|---|---|
List | ✅ | ❌ | ✅ | Ordered items, basic arrays |
Set | ❌ | ✅ | ✅ | Unique items, filters |
Map | ✅ | Keys | ✅ | Key-value storage |
Queue | ✅ | ❌ | ❌ | First-in-first-out processing |
Stack | ✅ | ❌ | ❌ | Backtracking, undo/redo |
LinkedList | ✅ | ❌ | ❌ | Custom nodes, fast inserts |
PriorityQueue | ✅ | ❌ | ✅ | Sorted dynamic processing |
✍️ Final Words
Dart gives you all the tools to build fast, flexible, and intelligent apps — only if you master data structures.
Practice. Refactor. Solve Real Problems. That’s how you grow as a Dart & Flutter developer 💪