Back to results for “Architecture”
Flutter › Flutter Architecture Patterns (MVVM & Clean Architecture)

What is the MVVM, or Model View ViewModel, architectural pattern, and how does it help clearly separate a Flutter app's user interface code from its underlying business logic?

Beginner

Answer

MVVM separates an application into three distinct, clearly defined parts, the Model representing the actual underlying data and core business rules, the View representing the visual widgets actually responsible for displaying that data to the user, and the ViewModel acting as an intermediary that prepares and exposes data specifically formatted for the View while also handling any user interactions, and this clean separation means the View itself can generally remain fairly simple and focused purely on layout and presentation, while the ViewModel handles the more meaningfully complex actual business logic, making both considerably easier to test and maintain fully independently of each other.
class ProductViewModel extends ChangeNotifier {
  List<Product> products = [];

  Future<void> loadProducts() async {
    products = await repository.fetchProducts();
    notifyListeners();
  }
}
Real-world example A product listing screen keeps its actual widget code focused purely on layout and displaying data, while a separate ProductViewModel handles fetching, sorting, and properly filtering the underlying product data, making the actual business logic considerably easier to test entirely independently of the user interface itself.

Common follow-ups: How does the ViewModel actually communicate a state change back to the View that needs to rebuild?;What is the difference between MVVM and the closely related MVC pattern?