Data Structure in Dart – Part 2: Advanced Data Structures in Flutter Architecture

16. State Normalization (Redux / Large Apps)

Problem

Large Flutter apps often suffer from:

  • Deeply nested objects
  • Hard-to-update UI
  • Excessive rebuilds

Solution → Normalize State

Use Maps instead of deep trees.

Bad

User {
  List<Post> posts;
}

Good

Map<String, User> users;
Map<String, Post> posts;

Why?

  • O(1) access
  • Easier updates
  • Smaller rebuild scope

Data Structure Used: HashMap


17. Event-Driven Architecture (Streams as Queues)

Flutter is Event-Based

  • User taps
  • API responses
  • WebSocket messages

All of these form an event queue.


Stream = Observer + Queue

final controller = StreamController<Event>();

Internally:

[Event1] → [Event2] → [Event3] → UI

Used in:

  • BLoC
  • Riverpod StreamProvider
  • WebSocket listeners

18. Background Tasks & Isolates (Message Queues)

Problem

Heavy computation blocks UI.

Solution → Isolates

Isolates communicate via message queues.

ReceivePort receivePort = ReceivePort();
await Isolate.spawn(worker, receivePort.sendPort);

Data Structures

  • Queue (messages)
  • Immutable data (safe transfer)

19. Image & Asset Caching (LRU in Practice)

Flutter Image Cache

Flutter internally uses:

  • HashMap
  • LRU eviction

You should mirror this for:

  • API images
  • WebView content
  • Offline files

Custom Cache Layer

final imageCache = LRUCache<String, Uint8List>(100);

Rules:

  • Fixed capacity
  • Evict least-used
  • Prevent memory leaks

20. Offline-First Architecture

Data Flow

UI → Cache → API → Cache → UI

Data Structures

LayerStructure
Local DBB-Tree (SQLite)
Memory CacheHashMap
Sync QueueQueue
Conflict trackingSet

Sync Queue Example

Queue<SyncTask> pendingTasks = Queue();
  • Retry failed requests
  • Process sequentially
  • Resume on app restart

21. Pagination at Scale

Wrong Approach

  • Load everything into List
  • Rebuild entire widget tree

Correct Approach

  • Windowed lists
  • Lazy loading
  • Indexed access

Structures Used

PurposeDS
Visible itemsList
Prefetch bufferQueue
Loaded pagesMap<int, Page>

22. Flutter Rendering Pipeline

Internals

StageData Structure
Widget TreeTree
Element TreeTree
Render TreeTree
Layer TreeTree

Understanding this helps you:

  • Reduce rebuilds
  • Avoid layout thrashing
  • Optimize animations

23. Dependency Injection

Providers Form a Graph

ProviderA → ProviderB → ProviderC
  • Circular dependencies = graph cycle
  • Riverpod prevents invalid cycles

Data Structure: Directed Acyclic Graph (DAG)


24. Feature Flags & Permissions

Efficient Access

Set<String> enabledFeatures;

Why Set?

  • O(1) lookup
  • No duplicates
  • Clean semantics

Used for:

  • A/B testing
  • Remote config
  • Role-based UI

25. Error Handling Pipelines

Error Flow

Error → Queue → Logger → UI

Use:

  • Queue for retries
  • Stack for call traces
  • Map for error metadata

26. Large Forms & Validation Trees

Form as Tree

Form
 ├── Section
 │    ├── Field
 │    └── Field

Traversal:

  • DFS → validate all
  • BFS → step-by-step wizard

27. Performance Checklist (DS-Focused)

SymptomLikely DS Issue
UI jankLarge List rebuild
Memory leakUnbounded cache
Slow searchLinear scan instead of Map/Trie
Crashes on backStack overflow
API spamMissing cache

28. How Senior Flutter Devs Think

They don’t ask:

“Which package should I use?”

They ask:

“What data structure models this problem best?”


29. Architecture Mapping Summary

Flutter ConcernData Structure
NavigationStack
StateMap
UITree
CacheLRU
SearchTrie
EventsQueue
DependenciesGraph
Offline syncQueue + Set

30. Final Takeaway

Flutter is not about widgets.
It is about data flowing through well-chosen structures.

If you master this:

  • Your apps scale
  • Your UI stays smooth
  • Your architecture stays clean

Để lại một bình luận

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *

Lên đầu trang