A production-ready, highly scalable Flutter REST API reference project demonstrating senior engineering patterns: Clean Architecture, SOLID Principles, Riverpod State Management, and Dio Networking with Material 3 Design.
This repository demonstrates how to architect a real-world enterprise Flutter application with robust networking capabilities:
- Clean Architecture & Feature-First Directory Structure
- Generic
ApiClientWrapper forGET,POST,PUT,PATCH,DELETE,Multipart Upload, andFile Download - Dio Interceptors Stack: Logging, Authorization, Token Refresh with Request Locking, Retry with Exponential Backoff, and Domain Error Interceptors
- JWT Token Lifecycle Management: Access token injection, auto-refresh on 401 Unauthorized, queueing concurrent requests, and secure local storage
- Domain Exception Mapping: Converts network outages, timeouts, and status codes (400, 401, 403, 404, 409, 422, 500) into user-friendly UI error notifications
- In-Memory Caching: TTL expiration strategy preventing unnecessary remote HTTP requests
- Pagination Patterns: Infinite scrolling, Pull-to-Refresh, Load-More, and dual support for Page-Based and Cursor-Based pagination strategies
- Material 3 UI: Shimmer loading skeletons, empty states, error retry widgets, and dynamic Light/Dark mode toggling
βββββββββββββββββββββββββββββββ
β Presentation Layer β
β (Widgets, Screens, UI) β
ββββββββββββββββ¬βββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β State Management Layer β
β (Riverpod StateNotifiers) β
ββββββββββββββββ¬βββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Domain & Repository β
β (Interfaces, Entities) β
ββββββββββββββββ¬βββββββββββββββ
β
βΌ
βββββββββββββββββββββββββββββββ
β Data & Network Layer β
β (Dio, ApiClient, Storage) β
βββββββββββββββββββββββββββββββ
sequenceDiagram
autonumber
actor UI as Presentation (Riverpod)
participant AC as ApiClient
participant AI as AuthInterceptor
participant TRI as TokenRefreshInterceptor
participant RI as RetryInterceptor
participant EI as ErrorInterceptor
participant API as Remote REST Server
UI->>AC: Execute HTTP Request
AC->>AI: Attach Authorization: Bearer Token
AI->>TRI: Forward Request Options
TRI->>RI: Forward Request
RI->>API: Transmit over Network
alt Successful Response (2xx)
API-->>AC: Return JSON Response
AC-->>UI: ApiResult.success(data)
else 401 Unauthorized (Token Expired)
API-->>TRI: 401 Unauthorized
TRI->>TRI: Lock Request Queue & Execute Token Refresh API
TRI->>API: Retry original request with new Access Token
API-->>AC: Return JSON Response
else Network / Transient Server Error (5xx)
API-->>RI: Connection Timeout or 500
RI->>RI: Exponential Backoff Delay & Retry
RI->>API: Retry Request
else Terminal API Error
API-->>EI: 400 / 403 / 404 / 422 / 500
EI->>AC: Wrap in Domain ApiException
AC-->>UI: ApiResult.failure(userFriendlyMessage)
end
lib/
βββ main.dart # App entry point, Flutter binding & ProviderScope
βββ app.dart # Main shell, Material 3 Theme, Navigation shell
βββ core/ # Cross-cutting core architecture modules
β βββ config/
β β βββ env_config.dart # Multi-environment config (Dev, Staging, Prod)
β β βββ app_constants.dart # Storage keys, Endpoints, Headers, Pagination defaults
β β βββ theme_config.dart # Material 3 light & dark theme setup
β βββ network/
β β βββ api_client.dart # Generic Dio HTTP API client wrapper
β β βββ api_response.dart # ApiResponse<T>, PaginatedResponse<T>, ApiResult<T>
β β βββ api_exception.dart # ApiException hierarchy & error message converters
β β βββ cache_manager.dart # In-memory TTL response cache manager
β β βββ interceptors/
β β βββ logging_interceptor.dart # Formatted console logging
β β βββ auth_interceptor.dart # Dynamic Bearer header injection
β β βββ token_refresh_interceptor.dart # Thread-safe 401 token refresh handler
β β βββ error_interceptor.dart # Dio to Domain ApiException converter
β β βββ retry_interceptor.dart # Exponential backoff retry handler
β βββ services/
β β βββ storage_service.dart # Secure token & user session storage
β β βββ connectivity_service.dart# Real-time network status detector
β β βββ download_service.dart # Device downloads path generator
β βββ models/
β β βββ user_model.dart # User entity DTO & JSON converters
β β βββ auth_model.dart # Auth tokens & Login payload models
β β βββ pagination_model.dart # Page & Cursor pagination parameters
β βββ repositories/
β β βββ auth_repository.dart # Abstract interface & impl for Authentication
β β βββ user_repository.dart # Abstract interface & impl for User CRUD
β β βββ file_repository.dart # Abstract interface & impl for Upload/Download
β βββ providers/
β βββ core_providers.dart # Riverpod dependency injection registry
βββ features/ # Feature-first modular components
β βββ auth/ # Authentication feature
β β βββ presentation/login_screen.dart
β β βββ providers/auth_provider.dart
β βββ users/ # User management & directory
β β βββ presentation/
β β β βββ user_list_screen.dart
β β β βββ user_detail_screen.dart
β β β βββ user_form_dialog.dart
β β βββ providers/user_provider.dart
β βββ media/ # File Upload & Download showcase
β β βββ presentation/
β β β βββ upload_demo_screen.dart
β β β βββ download_demo_screen.dart
β β βββ providers/file_provider.dart
β βββ pagination/ # Pagination strategy showcase
β β βββ presentation/pagination_demo_screen.dart
β βββ theme/
β βββ providers/theme_provider.dart
βββ shared/ # Reusable UI widgets & state views
βββ widgets/
βββ skeleton_loader.dart # Shimmer skeleton loaders
βββ empty_view.dart # Clean empty state widget
βββ error_view.dart # Error retry view widget
βββ custom_button.dart # Primary & Outlined action button with loading spinner
βββ custom_text_field.dart # Styled M3 text field with validation
| Package | Purpose |
|---|---|
flutter_riverpod |
Reactive state management & dependency injection |
dio |
Powerful HTTP client with Interceptors, Form-data, and CancelTokens |
flutter_secure_storage |
Encrypted storage for access & refresh JWT tokens |
shared_preferences |
Key-value local storage for app settings & theme preference |
shimmer |
Material 3 skeleton loading placeholders |
connectivity_plus |
Real-time network connectivity checking |
path_provider |
Cross-platform filesystem path resolution |
mocktail |
Type-safe mocking library for unit testing |
- Flutter SDK
^3.19.0or later - Dart SDK
^3.3.0or later
-
Clone the repository:
git clone https://github.com/your-username/flutter-rest-api.git cd flutter-rest-api -
Install pub packages:
flutter pub get
-
Run code analyzer:
flutter analyze
-
Execute unit tests:
flutter test -
Launch Application:
flutter run
-
SOLID Principles:
- Single Responsibility: Each class has one focused duty (e.g.
AuthInterceptoronly injects headers,UserListNotifieronly manages list state). - Open/Closed: Repository interfaces (
IUserRepository,IAuthRepository) allow extending implementations without mutating clients. - Liskov Substitution: Storage implementations are fully interchangeable.
- Interface Segregation: Clean, unbloated contracts for repositories.
- Dependency Inversion: High-level modules depend on abstractions via Riverpod providers.
- Single Responsibility: Each class has one focused duty (e.g.
-
Robust Network Resilience:
- Automatic retries with exponential backoff on transient network drops.
- Seamless token refreshing without interrupting user interactions.
- Graceful offline fallback messaging.
-
Zero Secrets & Security:
- Tokens stored in encrypted OS keychain / key store.
- Base URLs isolated inside
EnvConfig. - Zero hardcoded passwords or API keys.
- Add OAuth2 Authorization Code flow support.
- Implement GraphQL API client comparison module.
- Add integration test suite with
integration_test.
This project is licensed under the MIT License - see the LICENSE file for details.