Frontend
1,843 installs
Managing State in Flutter
by flutter/skills
Manages application and ephemeral state in a Flutter app. Use when sharing data between widgets or handling complex UI state transitions.
Skill content
Manage ephemeral and app-level state in Flutter using StatefulWidget, MVVM, and Provider. - Distinguishes between ephemeral state (single widget, managed with setState()) and app state (shared across widgets, managed with MVVM and provider package) - Implements unidirectional data flow with a single source of truth: Models handle data, ViewModels manage UI state via ChangeNotifier, Views consume and display state - Provides sequential workflow for MVVM implementation: define Repository, create ViewModel, inject via ChangeNotifierProvider, consume state with Consumer or context.read() - Includes complete code examples for both ephemeral state patterns and app-state management with async operations, loading states, and error handling Managing State in Flutter Contents - Core Concepts - Architecture and Data Flow - Workflow: Selecting a State Management Approach - Workflow: Implementing MVVM with Provider - Examples Core Concepts Flutter's UI is declarative; it is built to reflect the current state of the app (UI = f(state)). When state changes, trigger a rebuild of the UI that depends on that state. Distinguish between two primary types of state to determine your management strategy: - Ephemeral State (Local State): State contained neatly within a single widget (e.g., current page in a PageView, current selected tab, animation progress). Manage this using a StatefulWidget and setState(). - App State (Shared State): State shared across multiple parts of the app and maintained between user sessions (e.g., user preferences, login info, shopping cart contents). Manage this using advanced approaches like InheritedWidget, the provider package, and the MVVM architecture. Architecture and Data Flow Implement the Model-View-ViewModel (MVVM) design pattern combined with Unidirectional Data Flow (UDF) for scalable app state management.