Flutter4 min read

Why I Switched from MVC to MVVM in Flutter

Why I switched from MVC to MVVM in Flutter, the challenges I faced, and how I structure my projects for better maintainability and testability.

From MVC to MVVM in Flutter

When I first started building applications using Flutter, the project architecture I frequently used was MVC (Model-View-Controller). It was a fairly simple architecture that met my needs at the time. Back then, I didn’t give much thought to architecture; as long as the features worked well and the code was tidy, I felt that was enough.

However, as the app I was working on grew larger and more complex, the MVC pattern actually made the project increasingly complicated. Just imagine a product page that initially only required fetching product data from an API and displaying it; later, it grew to include requirements like pagination, search, product filtering, error handling, caching, and various other business needs.

So, where exactly should all the business logic reside? It’s easy for a controller to inadvertently end up handling everything—from fetching data, managing state changes, and processing responses, to updating the user interface (UI). In reality, the issue doesn’t lie with the MVC pattern itself, but rather with the fact that the boundaries defining each component’s responsibilities often become blurred in actual implementation. And that is what sparked my interest in learning about MVVM.

What Makes MVVM Different?

MVVM Data Flow
MVVM stands for Model-View-ViewModel. For me, the main difference is how the ViewModel exposes presentation state to the View. Instead of manually coordinating UI updates, the View observes the state and reacts whenever it changes. The division of responsibilities is quite straightforward:

  • Model: Handles data.
  • View: Displays data.
  • ViewModel: Prepares and manages the state required by the View.

For example, when a user presses a refresh button, the View calls a method on the ViewModel. The ViewModel then requests data from a repository and updates its state. Subsequently, the View rebuilds itself to reflect the changes that have occurred, and the widgets do not need to know how the API works or how the data is retrieved.

How I Structure My Projects

I prefer organizing code by feature (feature-first) rather than grouping all models, views, and ViewModels into separate, global folders.

features/
└── product/
    ├── data/
    │   ├── models/
    │   ├── datasources/
    │   └── repositories/
    └── presentation/
        ├── views/
        ├── viewmodels/
        └── widgets/

When a situation calls for the development of a feature, most of the code I need is already located in one place. I also don’t always include a domain layer for every feature; it all depends on the complexity of the business logic being handled.

MVVM in Practice

Below is an example of a ViewModel responsible for fetching product data.

class ProductViewModel extends ChangeNotifier {
  final ProductRepository repository;

  ProductViewModel(this.repository);

  List<Product> products = [];
  bool isLoading = false;
  String? error;

  Future<void> fetchProducts() async {
    if (isLoading) return;

    isLoading = true;
    error = null;
    notifyListeners();

    try {
      products = await repository.getProducts();
    } catch (_) {
      error = 'Failed to load products';
    } finally {
      isLoading = false;
      notifyListeners();
    }
  }
}

On the View side, I only need to observe state changes.

ListenableBuilder(
  listenable: viewModel,
  builder: (context, child) {
    if (viewModel.isLoading) {
      return const CircularProgressIndicator();
    }

    if (viewModel.error != null) {
      return Text(viewModel.error!);
    }

    return ProductList(
      products: viewModel.products,
    );
  },
);

In this example, I use Flutter's built-in ChangeNotifier and ListenableBuilder to keep the implementation simple, without relying on any additional state management packages.

MVVM itself is not tied to a specific state management solution. The same separation of responsibilities can be achieved using Provider, Riverpod, or BLoC/Cubit.

Another aspect I appreciate about this approach is how easy it makes testing. I can use a fake repository to test the ViewModel's behavior without needing to render widgets or perform actual API requests.

Would I Still Use MVC?

Sure. Especially for smaller projects. MVVM doesn't automatically make your code better. You can still end up with a massive ViewModel if you put every piece of logic inside it. What matters is knowing which component should be responsible for what.

For now, MVVM is the approach I prefer when working with Flutter applications that I expect to maintain for a long time. It fits the way Flutter handles reactive UI, and I find it easier to reason about the code. That's pretty much why I made the switch.