Platform: Flutter 3.x
State Management: Bloc/Cubit
Local Database: Isar
Last Updated: 2025-10-18
State Management: Bloc/Cubit
Local Database: Isar
Last Updated: 2025-10-18
๐ฏ Architecture Overview
MStore Mobile menggunakan Pure Local + SWR (Stale-While-Revalidate) architecture untuk memastikan instant load dan offline-first experience.๐๏ธ Layer Architecture
1. Presentation Layer (UI)
Widgets
- StatelessWidget untuk UI statis
- StatefulWidget untuk UI dengan state
- Custom widgets untuk reusability
State Management
- Bloc/Cubit untuk business logic
- BlocBuilder untuk reactive UI
- BlocListener untuk side effects
2. Business Logic Layer (Bloc)
Responsibilities:- Handle user interactions
- Manage UI state
- Call service layer
- Transform data for UI
3. Service Layer (Pure Local + SWR)
Responsibilities:- Pure local data access (Isar)
- Background sync orchestration
- Connectivity management
- Error handling
4. Repository Layer
Responsibilities:- API communication (Dio/Retrofit)
- Data transformation (DTO โ Model)
- Error handling
- Request/Response mapping
5. Data Layer (Isar)
Responsibilities:- Local data persistence
- CRUD operations
- Query & indexing
- Reactive streams
๐ Data Flow
Read Flow (Pure Local)
1
User Action
User membuka screen โ Bloc emit
LoadData event2
Service Call
Bloc call
service.getData() โ Service read dari Isar3
Instant Return
Service return data dari Isar (< 10ms) โ Bloc emit
DataLoaded4
Background Sync
Service trigger background refresh (non-blocking)
5
Silent Update
API fetch โ Update Isar โ Isar stream notify UI โ Auto-update
Write Flow (Offline-First)
1
User Action
User submit form โ Bloc emit
SaveData event2
Write to Isar
Service write to Isar immediately โ Return success
3
Queue Sync
Service queue data untuk background sync
4
Background Sync
When online โ Sync to API โ Update Isar with server response
5
Conflict Resolution
If conflict โ Show conflict resolution UI
๐ Implementation Status
| Data Type | Status | File | Pattern |
|---|---|---|---|
| Products | โ Done | lib/core/product/product_service.dart | SWR |
| Inventory | โ Done | lib/core/product/product_service.dart | SWR |
| Branches | โ Done | lib/core/branches/branches_service.dart | Pure Local + SWR |
| Transaction History | โ Done | lib/core/transaction/transaction_service.dart | Pure Local + SWR |
| New Transactions | โ Done | lib/features/checkout/bloc/checkout_bloc.dart | Offline-First |
| Settings | โ Template | lib/core/settings/settings_service.dart | Pure Local + SWR |
| Reports | โ Template | lib/core/reports/reports_service.dart | Pure Local + SWR |
๐จ UI Patterns
No Loading Spinner
Isar Stream for Auto-Update
Pull-to-Refresh
๐ง Key Technologies
Flutter 3.x
Cross-platform mobile framework
Bloc/Cubit
State management dengan reactive pattern
Isar Database
Fast, local NoSQL database
Dio + Retrofit
HTTP client dengan interceptors
Get_it
Dependency injection container
Freezed
Code generation untuk immutable models
Dartz
Functional programming (Either, Option)
Connectivity Plus
Network connectivity detection
๐ฑ Development Setup
Prerequisites
Installation
Code Generation
๐งช Testing
Unit Tests
Widget Tests
Integration Tests
๐ Related Documentation
Pure Local Architecture
Detailed SWR pattern implementation
State Management
Bloc/Cubit patterns & best practices
Isar Database
Local database schema & queries
Networking
Dio, Retrofit, interceptors
Offline Implementation
Step-by-step offline-first guide
Testing Guide
Unit, widget, integration tests
๐ Project Structure
๐ฏ Best Practices
1. Always Use Pure Local Pattern
1. Always Use Pure Local Pattern
- Read dari Isar first (instant)
- Trigger background sync (non-blocking)
- Never wait for API response
2. Proper Error Handling
2. Proper Error Handling
- Use
Either<Failure, Success>pattern - Silent fail untuk background sync
- Show error hanya untuk user actions
3. Efficient State Management
3. Efficient State Management
- Keep Bloc logic simple
- Use Cubit untuk simple state
- Avoid nested BlocBuilders
4. Optimize Isar Queries
4. Optimize Isar Queries
- Use indexes untuk frequent queries
- Limit query results
- Use lazy loading untuk large lists
5. Code Generation
5. Code Generation
- Always run build_runner after model changes
- Use freezed untuk immutable models
- Use retrofit untuk type-safe API
๐ Performance Tips
Lazy Loading
Load data incrementally dengan pagination
Image Caching
Use cached_network_image untuk images
Widget Optimization
Use const constructors, avoid rebuilds
Background Isolates
Heavy computation di isolate terpisah
Dokumentasi ini adalah living document. Update sesuai dengan evolusi architecture dan best practices.