13. Testing, CI/CD & Architecture

Clean Architecture & Production Readiness

Engineer scalable mobile enterprise apps: Feature-first Clean Architecture (Presentation, Domain, Data layers), multi-environment flavor configs with compile-time constants, and automated Fastlane CI/CD release pipelines.

1. Feature-First Clean Architecture

Enterprise Flutter codebases maintain long-term maintainability by enforcing the Dependency Inversion Principle: business logic must never depend directly on UI widgets or external database packages.

1. Presentation Layer (Flutter UI) 2. Domain Layer (Pure Dart - Zero Framework Dependencies) 3. Data Layer (Concrete Implementations) Calls Implements Operates on Calls Calls Widgets, Screens, Theme BLoCs, Cubits, Riverpod Notifiers Business Entities (Pure Value Objects) Use Cases / Interactors (e.g. ExecuteCheckout) Repository Interfaces (Contracts) Repository Implementations Data Sources (Dio Remote API, SQLite DB) DTO Models & JSON Parsers

2. Canonical Directory Structure: Feature-First

lib/
├── app/
│   ├── app.dart
│   └── router.dart
├── core/
│   ├── constants/
│   ├── network/ (Dio client, Interceptors)
│   ├── theme/
│   └── utils/
└── features/
    └── authentication/
        ├── data/
        │   ├── datasources/
        │   │   ├── auth_remote_datasource.dart
        │   │   └── auth_local_secure_storage.dart
        │   ├── models/ (auth_dto.dart, auth_dto.g.dart)
        │   └── repositories/
        │       └── auth_repository_impl.dart
        ├── domain/
        │   ├── entities/ (user_entity.dart)
        │   ├── repositories/ (auth_repository.dart - Interface!)
        │   └── usecases/ (login_usecase.dart, logout_usecase.dart)
        └── presentation/
            ├── controllers/ (auth_notifier.dart / auth_bloc.dart)
            ├── screens/ (login_screen.dart, register_screen.dart)
            └── widgets/ (auth_text_field.dart, social_signin_button.dart)

3. Multi-Environment Flavor Configuration

Never hardcode development and production backend URLs directly into your codebase. Use --dart-define or --dart-define-from-file for compile-time environment injection:

// lib/core/config/app_environment.dart
enum AppFlavor { dev, staging, prod }

class AppEnvironment {
  static const AppFlavor flavor = AppFlavor.values.byName(
    String.fromEnvironment('FLAVOR', defaultValue: 'dev'),
  );

  static const String apiBaseUrl = String.fromEnvironment(
    'API_BASE_URL',
    defaultValue: 'https://dev-api.example.com/v1',
  );

  static const bool enableLogging = bool.fromEnvironment(
    'ENABLE_LOGGING',
    defaultValue: true,
  );
}

CLI Build Commands

# Run Development Flavor
flutter run --dart-define=FLAVOR=dev --dart-define=API_BASE_URL=https://dev.api.com

# Build Production Release Bundle
flutter run --release --dart-define=FLAVOR=prod --dart-define=API_BASE_URL=https://api.example.com

4. Automated CI/CD Deployment with Fastlane & GitHub Actions

Automate linting, testing, and store deployment using Fastlane:

git push to main branch GitHub Actions CI Pipeline flutter analyze & flutter test Fastlane Build & Code Sign Apple App Store (TestFlight) Google Play Store (Internal Track)

Fastlane Fastfile Configuration

# android/fastlane/Fastfile
default_platform(:android)

platform :android do
  desc "Deploy production App Bundle to Google Play Internal track"
  lane :deploy_internal do
    sh("flutter build appbundle --release --dart-define=FLAVOR=prod")
    upload_to_play_store(
      track: 'internal',
      aab: '../build/app/outputs/bundle/release/app-release.aab'
    )
  end
end

❓ Knowledge Check

Knowledge Check

Why does the Domain Layer in Clean Architecture contain zero imports from package:flutter/material.dart?

Knowledge Check

What is the advantage of using --dart-define over writing hardcoded environment variables in Dart files?

On this page