Official Documentation
Untuk dokumentasi lengkap, kunjungi:
Ecosystem & Plugins:
Architecture
Gunakan Clean Architecture dengan BLoC (Business Logic Component) untuk state management.
-
lib
-
core/ # Shared utilities, theme, constants
-
features
-
auth
-
data/ # Repositories, models, datasources
-
domain/ # Entities, use cases
-
presentation/ # BLoC, screens, widgets
-
-
-
main.dart
-
injection.dart # GetIt DI setup
-
State Management
Gunakan flutter_bloc (Cubit untuk simple, Bloc untuk complex):
class AuthCubit extends Cubit<AuthState> {
AuthCubit(this._repo) : super(AuthInitial());
final AuthRepository _repo;
Future<void> login(String email, String password) async {
emit(AuthLoading());
try {
final user = await _repo.login(email, password);
emit(AuthSuccess(user));
} catch (e) {
emit(AuthError(e.toString()));
}
}
}
Navigation
Gunakan GoRouter untuk declarative routing:
final router = GoRouter(
routes: [
GoRoute(path: '/', builder: (_, __) => HomeScreen()),
GoRoute(path: '/login', builder: (_, __) => LoginScreen()),
],
);
Dependency Injection
Gunakan GetIt untuk DI:
final getIt = GetIt.instance;
void setupDependencies() {
getIt.registerSingleton<AuthRepository>(AuthRepositoryImpl());
getIt.registerFactory(() => AuthCubit(getIt()));
}
Testing
# Unit test
flutter test
# Integration test
flutter test integration_test/
Code Quality
# Format
dart format .
# Analyze
dart analyze
# Linter (di pubspec.yaml)
analyzer:
# Konfigurasi rule linter (contoh: exclude files, dll)
API Integration (Networking)
Gunakan Dio dipadukan dengan Retrofit untuk type-safe HTTP client:
@RestApi(baseUrl: "https://api.example.com")
abstract class ApiClient {
factory ApiClient(Dio dio, {String baseUrl}) = _ApiClient;
@GET("/users")
Future<List<User>> getUsers();
}
Local Storage
- SharedPreferences: Untuk simple key-value pairs (auth token, theme).
- Hive atau Isar: Untuk local database / caching complex objects.
- Flutter Secure Storage: Wajib untuk data sensitif (tokens, passwords).
Code Generation
Gunakan build_runner dengan freezed dan json_serializable untuk immutable models:
@freezed
class User with _$User {
const factory User({
required String id,
required String email,
}) = _User;
factory User.fromJson(Map<String, dynamic> json) => _$UserFromJson(json);
}
Run codegen: dart run build_runner build --delete-conflicting-outputs
Build Flavors (Environments)
Gunakan flavors (Android) dan schemes (iOS) untuk memisahkan config Dev, Staging, dan Prod:
# Run dengan flavor tertentu
flutter run --flavor dev -t lib/main_dev.dart
flutter run --flavor prod -t lib/main_prod.dart
CI/CD Pipeline
Contoh pipeline dasar menggunakan GitHub Actions:
name: Flutter CI
on: [push, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: subosito/flutter-action@v2
with:
channel: "stable"
- run: flutter pub get
- run: dart format --output=none --set-exit-if-changed .
- run: flutter analyze
- run: flutter test