Search code examples
flutterarchitectureclean-architecture

Flutter way to implement clean architecture


I have started learning flutter after 5 years of android dev. And right now i am trying to implement multi modular clean architecture as i used to, when developing android, example of such architecture: https://github.com/android10/Android-CleanArchitecture

And i have ran into two issues i can't solve by on my own.

1) What type of flutter module should i use module/plugin/package? I want to make domian layer as simple as it could be(list of interfaces,mappers and dto's), in android it was written in plain kotlin without any android sdk's usage. It seems like domain layer should be package(because it doesn't have any platform specific code and seems to be the simplest module possible) and data layer should be plugin in case i would be forced to make some platform specific code here, but i am not sure. It would be great to have some suggestions here.

2) Maybe there is a github repo example, as I have shown above? All git repos i have found so far are implementing clean architecture with just folders and this approach always ends up with dependencies mess, loosing all the profit you can get from clean architecture. Any help is appreciated!


Solution

  • Recently I published an article on using DI container abstractions in a Modularized Flutter App adhering to Clean Architecture Principles. The package separation example in the article should give a decent overview of how to divide the solution into packages and organize dependencies between them to adhere to the Clean Architecture principles.

    Here are the short answers to your questions:

    1. The modules in your solution should be packages. It should be Dart-only packages for UI-agnostic layers like Domain and Flutter packages for layers like Presentation/UI.
    2. This Github repo is the example implementation of the Counter Flutter app with the solution division into physical Dart/Flutter packages instead of just the folders division. The diagram below depicts the packages you'll see in that example and the dependencies between them. I definitely recommend using physical layer division, i.e. using packages, in any serious Flutter app.

    Hope that helps you and others!

    The packages used in the example solution