Skip to content

Latest commit

 

History

2 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Why only the minimum widgets rebuild

I use Provider’s selection APIs (Selector) to ensure widgets only rebuild when the specific data they depend on changes.

  • [Completed count selection] lib/views/todo_view.dart:

    • Selector<AppStateViewModel, int>(selector: (context, vm) => vm.completedTodosCount, ...)
    • Depends only on a primitive int derived from todos. Only the count Text rebuilds when the computed value changes.
  • [List length selection] lib/views/todo_view.dart:

    • Selector<AppStateViewModel, int>(selector: (context, vm) => vm.appState.todos.length, ...)
    • The ListView container rebuilds only when the length changes (add/remove). Toggling a todo’s completed flag does not change length, so the container does not rebuild.
  • [Per-row selection] lib/views/todo_view.dart:

    • Each row is wrapped with Selector<AppStateViewModel, Todo>(selector: (context, vm) => vm.appState.todos[index], ...)
    • Only the row whose Todo instance changes rebuilds (e.g., when its completed flips).
    • Rows are keyed with ValueKey(todo.id), preserving identity across insert/remove operations and minimizing row re-mounting.
  • [Stateless item + non-listening actions] lib/views/custom_widget/todo_card.dart:

    • TodoCard is stateless and receives primitive props (task, completed).
    • Actions use context.read<AppStateViewModel>(), which does not listen, so dispatching actions does not trigger rebuilds.
  • [Derived, not duplicated state] lib/view_models/app_state_view_model.dart:

    • completedTodosCount is a getter derived from todos instead of a manually maintained counter, preventing drift and extra rebuilds.

Together, these choices ensure only the smallest necessary subtree rebuilds:

  • The count text when the computed count changes.
  • The list container only when length changes.
  • The specific TodoCard whose data changed.

About

The Dynamic State Shard Challenge

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages