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

1. Why Data Structures Matter in Flutter Architecture

Flutter apps are not just UI:

  • They manage state
  • They handle async data
  • They cache network responses
  • They render trees of widgets

Every Flutter app is built on data structures, whether you realize it or not.


2. Widget Tree = Tree Data Structure

Concept

Flutter UI is a tree:

MaterialApp
 └── Scaffold
     ├── AppBar
     └── Body
         └── ListView
             └── ListTile
  • Parent → child relationship
  • Depth-first rendering
  • Rebuilds propagate downward

Performance Insight

  • Deep trees → expensive rebuilds
  • Use const, Builder, Selector to reduce traversal

3. Navigation = Stack

Navigator Internals

Navigator.push(context, route); // push
Navigator.pop(context);         // pop

This is literally:

[ Home ]
[ Profile ]
[ Settings ] ← Top

Best Practices

  • Avoid unnecessary routes
  • Use popUntil to manage stack depth
  • Nested navigators = multiple stacks

4. State Management = Maps, Lists & Streams

Common Structures

Use CaseStructure
UI stateMap
CollectionsList
Event flowStream
FlagsSet

Example – Riverpod / Provider

final cartProvider = StateProvider<List<Product>>((ref) => []);
  • List → ordered items
  • Efficient rebuilds when list changes

5. Async Data Flow = Queue + Stream

Concept

  • API calls arrive over time
  • UI consumes data sequentially
API → Queue → Stream → UI

Example

StreamController<Message> controller = StreamController();

This acts like:

  • Queue → events
  • Observer → UI widgets

6. Caching in Flutter Apps (Critical)

Why Cache?

  • Reduce API calls
  • Improve UX
  • Offline support

LRU Cache for API Responses

final cache = LRUCache<String, ApiResponse>(50);

Used for:

  • Image caching
  • API responses
  • WebView content

7. Infinite Lists & Pagination

Data Structures Involved

FeatureStructure
Loaded itemsList
PagingQueue
CacheLRU

Example Pattern

if (!isLoading && hasMore) {
  fetchNextPage();
}

Avoid:

  • Duplicates
  • Unbounded memory growth

8. Forms & Undo Actions = Stack

Example

  • Text editing
  • Undo changes
  • Form steps
final undoStack = <FormState>[];

9. Search & Filter in Flutter

Search Bar

FeatureStructure
SuggestionsTrie
FiltersHashMap
SortingHeap

Example

List<Product> filtered = products
  .where((p) => p.name.contains(query))
  .toList();

10. Animations & Frames = Queue

Animation Pipeline

  • Frames queued
  • Rendered sequentially
  • Dropped if overloaded

Understanding this helps:

  • Avoid jank
  • Optimize animations

11. App Architecture Mapping

Clean Architecture

LayerDS
PresentationTree
DomainGraph
DataMap, Cache
NetworkQueue

MVVM / Redux

  • Store → Map
  • Reducers → Pure functions
  • Actions → Queue

12. Error Handling & Retry Logic

Retry Queue

Queue<Request> retryQueue = Queue();
  • Failed requests
  • Retry with backoff
  • Prevent UI blocking

13. Memory Optimization Tips

  • Clear unused lists
  • Limit cache size
  • Avoid global static collections
  • Use weak references (where applicable)

14. Debugging with DS Awareness

BugLikely Cause
UI freezeLarge list rebuild
Memory leakUnbounded cache
Duplicate UIStack misuse
Slow searchWrong DS

15. Final Takeaway

Great Flutter architecture is invisible data structure design.

If your data structures are right:

  • UI is smooth
  • Code is clean
  • Scaling is easy
  • Bugs are fewer

Để 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