A Flutter project implementing Clean Architecture principles and BLoC pattern for state management. The project features product management with data persistence using both SQLite and Hive.
Important
Project Purpose : This project serves as a practical example of implementing modern Flutter development practices and showcasing the usage of several important packages:
-
dartzPackage Usage- Implementing functional programming concepts
- Using
Eitherfor better error handling - Example:
Future<Either<Failure, List<Product>>> getProducts({bool useCache = true});
-
freezedPackage Implementation- Creating immutable data classes
- Pattern matching with sealed unions
- Example:
@freezed class Product with _$Product { const factory Product({ required String id, required String name, required int price, }) = _Product; }
-
injectableDependency Injection- Automatic dependency injection setup
- Clean dependency management
- Example:
@injectable class GetProducts implements UseCase<List<Product>, bool> { final ProductRepository repository; GetProducts(this.repository); }
-
json_serializableUsage- Automated JSON serialization/deserialization
- Type-safe JSON handling
- Example:
@JsonSerializable() class Product { factory Product.fromJson(Map<String, dynamic> json) => _$ProductFromJson(json); Map<String, dynamic> toJson() => _$ProductToJson(this); }
- Understanding functional programming in Dart with
dartz - Implementing immutable state management using
freezed - Setting up dependency injection with
injectable - Managing JSON serialization with
json_serializable
The project follows Clean Architecture with the following layer structure:
lib/
├── core/
│ ├── error/
│ │ └── failures.dart
│ └── usecases/
│ └── usecase.dart
├── features/
│ └── product/
│ ├── domain/
│ ├── data/
│ └── presentation/
└── injection.dart
dependencies:
dartz: ^0.10.1
flutter_bloc: ^8.1.6
freezed_annotation: ^2.4.4
get_it: ^8.0.2
hive: ^2.2.3
hive_flutter: ^1.1.0
injectable: ^2.5.0
json_annotation: ^4.9.0
json_serializable: ^6.8.0
path: ^1.9.0
path_provider: ^2.1.5
sqflite: ^2.4.0
dev_dependencies:
build_runner: ^2.4.13
freezed: ^2.5.7
hive_generator: ^2.0.1
injectable_generator: ^2.6.2This layer contains:
Product: Core product model with fields:- id: Unique identifier
- name: Product name
- price: Product price
- stock: Available quantity
- categories: Product categories
- createdAt: Creation timestamp
ProductRepository: Core interface for data operations with methods:getProducts: Retrieve product listsaveProduct: Save new product
GetProducts: Retrieve list of productsSaveProduct: Save new product
ProductLocalDataSource: Local data source with support for:- SQLite: Relational storage
- Hive: Key-value storage
ProductRepositoryImpl: Implementation of repository with error handling
ProductBloc: State management with events:fetchProducts: Get products listaddProduct: Add new product
States:
initial: Initial stateloading: Loading stateloaded: Data loaded stateerror: Error state
ProductListPage: Main page showing products listProductListItem: Widget for displaying individual product
Uses get_it and injectable for dependency injection:
DatabaseModule: Database configurations- SQLite setup
- Hive setup
- Uses
Eitherfromdartzpackage for error handling - Different
Failuretypes for error categorization
- Implements
flutter_blocfor state management - Separation of events and states using
freezed
- Dual storage system using SQLite and Hive
- Caching capabilities
Uses the following libraries for code generation:
freezed: For data classesinjectable: For dependency injectionhive_generator: For Hive adapters
- Install dependencies:
flutter pub get- Run code generators:
flutter pub run build_runner build- Run the app:
flutter run- Clear separation of concerns
- Independent layers
- Testable code structure
- Domain-driven design
- Predictable state changes
- Separation of UI and business logic
- Easy testing
- Reactive programming approach
- Dual storage system
- Efficient caching
- Type-safe data handling
- Error handling at all layers
- Service locator pattern
- Modular code structure
- Easy dependency management
- Auto-generated DI code
- Comprehensive error types
- Functional programming approach
- Type-safe error handling
- User-friendly error messages
- Type-safe programming with freezed
- Clean and maintainable code
- Following SOLID principles
- Extensive use of Flutter best practices
SQLite table structure for products:
CREATE TABLE products(
id TEXT PRIMARY KEY,
name TEXT,
price REAL,
stock INTEGER,
categories TEXT,
created_at TEXT
)- User triggers an action
- BLoC receives an event
- Use case is executed
- Repository handles data operation
- State is updated
- UI reflects changes
- Efficient data caching
- Lazy loading when appropriate
- Minimal rebuilds in UI
- Optimized database queries
- Fork the repository
- Create your feature branch
- Commit your changes
- Push to the branch
- Create a Pull Request
This project is licensed under the Apache License - see the LICENSE file for details.