---
title: "Discussion on \"Flutter MVVM and Clean Architecture - Part 1: Setup\" | Hashnode"
description: "Discussion on \"Flutter MVVM and Clean Architecture - Part 1: Setup\". I've written in the past an example of an iOS app, using  The Clean Architecture and MVVM, which is a solution that has worked very well for me and my teams in the past years.\nI'm going to share as a series of posts how easy and clean it can be scali..."
url: "https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Flutter MVVM and Clean Architecture - Part 1: Setup](https://cassiuspacheco.com/flutter-mvvm-and-clean-architecture-part-1-setup?utm_source=hashnode&utm_medium=feed)

I've written in the past an example of an iOS app, using The Clean Architecture and MVVM, which is a solution that has worked very well for me and my teams in the past years. I'm going to share as a series of posts how easy and clean it can be scali...

Nov 13, 2021 · 8 min read · cassiuspacheco.com  
Updated Dec 28, 2021  
Read the article

-   [#flutter](https://hashnode.com/tag/flutter)
-   [#dart](https://hashnode.com/tag/dart)
-   [#mvvm](https://hashnode.com/tag/mvvm)
-   [#clean-code](https://hashnode.com/tag/clean-code)

## Written by

[Cassius Pacheco](https://hashnode.com/@cassiuspacheco)

@cassiuspacheco · Sydney, Australia

  
[View profile](https://hashnode.com/@cassiuspacheco)

Brazilian Software Engineer living in Australia 🇧🇷 🇦🇺 | Dart | Flutter | Swift | iOS

My life changed in 2011 when I fell in love with iPhone and started working as an iOS Developer. I'm passionate about clean and effective code, code review, organisation, agile work, sharing knowledge and experiences, but above all I'm passionate about building awesome products.

## Responses 3

Markdown supported · ⌘↵ to post

[Daniele Chiomenti](https://hashnode.com/@jdaniele)

[Apr 26](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/69ee79fd3d6a492cdd4d5305)

Hi Cassius, first of all, thanks a lot for this very interesting series.

I have two main questions and I hope you can help me:

1.  how can we separate the viewmodel from the view, when the viewstate contains some variable related to flutter (that we don't want to access from the viewmodel)?

For example if a OverlayEntry or a TransformationController are part of the viewstate, and there is a logic in the viewstate controlling those variables, how would you manage that situation? We should not depend on flutter packages in the viewmodel, right? The state and its logic should be indipendent from the way the state is presented, right?

Look at this function in the viewstate for example (where the \_mmsl is the viewmodel):

void \_zoomToWaypoint(Offset pos) { final size = MediaQuery.of(context).size; final mapW = (\_mmsl.cameraOn && \_mmsl.viewSwapState == 0) ? size.width / 2 : size.width;

```
final px = Offset(pos.dx * mapW, pos.dy * size.height);
final newScale = (_mmsl.zoomLevel + 25) / 100;
final tx = mapW / 2 - px.dx * newScale;
final ty = size.height / 2 - px.dy * newScale;
final matrix = Matrix4.identity()
  ..translate(tx, ty)
  ..scale(newScale);
_mmsl.zoomLevel = _mmsl.zoomLevel + 25;
setState(() {
  _transformController.value = matrix;
});
```

}

1.  I'm very newbie on Flutter and I 'm trying to do a refactoring of a new internal project (to my company), so I'm studying hard, very hard! :) There is a page where there are two lists of objects (entities) to show. The viewmodel is shared between the main view and a custom widget (child of the main view)

Now, putting all inside the viewmodel, it means that every time I copy the state to add it to the sink, I have to shallow (or deep) copy all the entities in the list?

Do you think is efficient to do that, adding a state, full of variables and lists?

Should I just copy the reference of the two lists (actually leaving them the same)?

Could be the observer pattern (subscribers observing a subject) more efficient in this case?

The state is not copied, just modified inside the viewmodel, and the subject (viewmodel) just emits a synchronous event to the subscribers (calling their callback function). Finally the observers receiving the event, call setState() {() {}), just to refresh the pages.

I'm not still fully convinced that a single stream is the only way to go. For example, if you have a container showing a camera feed (30 fps) and the rest of page stays pretty the same, most of the time (but could change sometime), having a BIG StreamBuilder for the entire page, means that all the widgets of the page will be rebuilt at 30 fps, even if the rest of the state variables are not changed, just because the video stream is playing.

In that case, may be, we should expose two streams, one for the whole state, and one specific for the video stream, accessed by a variable inside the state. Is it right?

Thanks a lot. Best regards.

[Zahir Gudiño](https://hashnode.com/@zgudino)

[Feb 24, 2023](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/63f93aa25ebda1e9aa53d2f6)

Thank you Cassius for this amazing write up. You are BRILLIANT!

[Ali Ersöz](https://hashnode.com/@getinloop)

[Nov 29, 2021](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/61a44d162be89f3d0ab9f14f)

hey cassius, this is a great post. really appreciate it.

i am pretty new to flutter and have one question. how would you navigate to another screen after a post api call here? say the view state is updated after a login and now i can navigate to home screen. since i expect that to be communicated via state update. the only place appear to me to do this is just above where you return the `Scaffold` but that doesn't feel right, since it would be updating the view.

[Cassius Pacheco](https://hashnode.com/@cassiuspacheco)

[Nov 29, 2021](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/61a44d162be89f3d0ab9f14f#reply-61a497aa9ac0d611876b1890)

Hello, thanks for the feedback!

About routing, you really want to keep the logic about where and how you're navigating to in the view model, so you can unit test it as well as have complex routing systems depending on user interactions, like you mentioned, after an API call, for example.

Now, you don't want to access widgets, context, Navigators, etc in the view model, you only want the pure logic in there, such as the routing logic.

If you want a spoiler of the next post showing how I do it, have a look at the part 2 of the series code [github.com/CassiusPacheco/flutter\_mvvm/commit/fbd…](https://github.com/CassiusPacheco/flutter_mvvm/commit/fbd7237ed1f08993becf16ff7a80ec6aacee044f)

The post explaining it will be coming up soon. Hope that helps!

[Cassius Pacheco](https://hashnode.com/@cassiuspacheco)

[Nov 29, 2021](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/61a44d162be89f3d0ab9f14f#reply-61a4af649ac0d611876b1b68)

Good news Ali, I've just posted the part 2 of the series that explains routing [cassiuspacheco.com/flutter-mvvm-and-clean-archite…](https://cassiuspacheco.com/flutter-mvvm-and-clean-architecture-part-2-testable-routing-ckwkjnkph0dmngns1cf8e7d5n)

[Ali Ersöz](https://hashnode.com/@getinloop)

[Nov 29, 2021](https://hashnode.com/posts/flutter-mvvm-and-clean-architecture-part-1-setup/618f936d83e4212d6e57844c/comment/61a44d162be89f3d0ab9f14f#reply-61a4ce8e2be89f3d0ab9fc7c)

[Cassius Pacheco](https://hashnode.com/@cassiuspacheco)

hi cassius, this is great and thank you very much for the quick response. it is funny that right after sending this message I also came up with a similar solution. although in my case I opted in to having each view model to define its own possible routes, so that it doesn’t have the full power of accessing anywhere in the app. so it emits events from the view model (such as emitting `second` event to the `navigationEvents` publish subject) which gets observed in the view state and then view state does the navigation.

i liked the fact that you abstracted the observation part and put that to the base class. also liked the `AppRouteAction` enum you defined to even further abstract the routing.

great job for one of the most complete and clean flutter architecture post.
