Flutter – Clean Architecture & TDD

  API, Flutter App, Movie

Introduction

This project consists of a mobile application developed in Flutter, which shows information about movies by querying the themoviedb.org API.

Table of Contents

  1. Introduction
  2. goals
  3. Preview
  4. Project Installation
  5. Project Structure
  6. Clean Architecture
  7. Workflow
    • Presentation Layer
    • Domain Cloak
    • Data Layer
  8. TDD
  9. Unit Testing In Flutter (Unit Testing)

goals

  • The objective is to practice, share and discuss the topics learned on the Resocoder blog .
  • Consume the entire API from themoviedb.org .

Preview

Project Installation

Project Structure

├─ core/                       NÚCLEO
│  ├─ api/                     definición de elementos asociados a la API
│  ├─ errors/                  definición de errores y excepciones
│  ├─ network/                 utils asociados a la conexión de internet
│  ├─ usecases/                definición de los casos de uso
│  └─ routes.dart              rutas de nuestro proyecto
│
├─ data/                       CAPA DE DATOS
│  ├─ datasources/             origen de los datos solicitados por el repositorio
│  ├─ models/                  contienen funciones fromJson & toJson y heredan de una entidad
│  └─ repositories             implementación de los repositorios
│
├─ domain/                     CAPA DE DOMINIO
│  ├─ entities/                entidades
│  ├─ repositories/            definición de los repositorios
│  └─ usecases/                implementación de los casos de uso
│
├─ presentation/               CAPA DE PRESENTACIÓN
│  ├─ business_logic/          gestor de estados
│  ├─ views/                   vistas
│  └─ widgets/                 widgets personalizados utilizados en las vistas
│
├─ injection_container.dart    inyección de dependencias
└─ main.dart

Clean Architecture

The main idea in clean architecture is to separate the code into independent layers, which become more abstract when moving to the inner layers.

Because the inner layers represent rules that restrict the outer layers, which would be the dependency rule (The outer layers can depend on the inner ones, but not vice versa).

The idea would be to have the requirements defined, in addition to the entities and use cases that our project will contain. Having these clear rules, you can write those mechanisms that are necessary for the use case to be well executed.

Workflow

The user interacts with the views by triggering events, which when heard by our state manager, generate a new flow of states; they can be mutated to indicate what stage of the process it is in and thus allow the development of a use case.

Presentation Layer

Widgets make up our views and need state management to mutate as required during the life of the application.

This time flutter_bloc was used.

Domain layer

As can be seen in the image, the repository belongs to both the data layer and the domain layer, with the big difference that, in the domain layer there are only the abstract definitions of it and in the data layer there would be the implementation.

This will depend on a “contract” defined in the domain layer, which must be fulfilled in the data layer.

Data layer

It consists of an implementation of the repositories and data sources: remote (API) and local (CACHE).

In the repository, it is decided whether to return data from the api or those stored in the cache and when to store them.

In this layer you do not work with entities, you work with models, they inherit from an entity and have toJson and fromJson methods, you get the benefit, in case you decide to change from json to xml in the future, without having too many headaches.

TDD

Test Driven Development: It is an iterative development process, where the developer writes a test before writing enough code to meet it and then refactoring if necessary.

The advantage of this process is that the developer focuses more on the requirements of the software, wondering why he needs the fraction of code that he is about to write, before continuing with the implementation.

Through this process the developer can identify ill-defined requirements and improve their habits over time, leading to an improvement in their code quality.

Download Clean Architecture & TDD app source code on GitHub

https://github.com/sr-Te/Flutter-CleanArchitecture-TDD