Understanding of clean architecture in your flutter apps WITH TDD(TEST DRIVEN DEVELOPMENT)

Muhammad Mashood Siddiquie
3 min readNov 8, 2021

--

CLEAN ARCHITECTURE, as name suggest mostly devs thinks maybe a nightmare for them, It’s hard but it’s easy if it becomes handy and I promise after the series of blogs you will definitely gonna implement It in your next flutter big projects.

Mostly developer’s around the world once think what shall we do to make our code maintainable, scalable for future purpose. Mostly we developer’s right good code that works perfectly great, but maintaining that code after a long time gets hectic for us as we don’t work on creating a good architecture for our apps which makes code tightly coupled or hardly to understand again, that’s where we need to think what shall we do to make our code more maintainable, divide into different layers etc, that’s where “CLEAN ARCHITECTURE” comes handy.

WHAT IS TDD?

TDD stands for Test driven development, which means before writing our production code, we write code in parallel with our tests, if a test pass then our production will also be pass.

BASIC INTRODUCTION TO CLEAN ARCHITECTURE:

Clean architecture was introduced by Robert Martin, broadly known as “UNCLE BOB”. He has written multiple books for coding and best practices. He is most popular for his considerations on “CLEAN CODE”.

Clean architecture is divided into three layers:

  • DATA
  • DOMAIN
  • PRESENTATION


    Lets talk about the Domain First, what is a domain layer, domain layer is categorized into three sub folders:
  • Entity
  • Use case
  • Repository



    ENTITY: Entity is something which defines business set of rules.

    USE CASE: Use case is something which defines what type on functionality each business logic can have, Let’s take example of a simple todo app. What business functionality are included in todo app, we can “ADD” task,”Get” tasks,”UPDATE” tasks, “DELETE” tasks, the word inside inverted commas are our use cases’ hope it’s cleared what is a use case.

    REPOSITORY: This is an interface/abstract class with declaration of a function not implementation of a function, this concrete/repository created here will be implement inside our data layer.


    DATA LAYER:
    A data layer consist of there sub folders,
  • Datasources.
  • Models.
  • Repository


    DATA-SOURCES: Data source consist of two folders, remote and local, here we define app logics depending upon wether we need data from the api or from our mobile local storage.

    MODELS: This model is only respective to our data layer, the difference between a model and entity is, model define here contain fromJson and toJson method whereas entity doesn’t, model extends entity but entity can not extend a model, this makes separation of layers and easy to maintain code.

    REPOSITORY: Here we implement the repository we define inside our domain layer, here we take remote and local datasource as our argument also we catch exceptions as early as possible rather then inside our presentation layer.


    PRESENTATION LAYER:
    It consist of our state management and UI presentation of our app.


    I hope so you have understand the basic of clean architecture for flutter apps.


    What will be the project structure of flutter application using clean architecture, let’s see here:

DON’T WORRY ABOUT THE OTHER FOLDERS DEFINE HERE, WE WILL DISCUSS THIS ON OUR NEXT ARTICLE.

This is just a beginning to learn something new and I wish you will enjoy the whole series of clean architecture in flutter app.



In the next blog we will discuss about dependencies and other folder structures defined here, I promise you will love it and start implementing the clean architecture in your next flutter apps project.


Thank you for reading this blogpost, I hope you like this and if you like this then surely do like this post so I may know I am helping other people to write and practice good code.

--

--