flutter-apply-architecture-best-practices
작성자: flutter
권장되는 계층적 접근 방식(UI, 로직, 데이터)을 사용하여 Flutter 애플리케이션을 설계합니다. 새 프로젝트를 구조화하거나 확장성을 위해 리팩토링할 때 사용하세요.
npx skills add https://github.com/flutter/skills --skill flutter-apply-architecture-best-practicesArchitecting Flutter Applications
Contents
Architectural Layers
Enforce strict Separation of Concerns by dividing the application into distinct layers. Never mix UI rendering with business logic or data fetching.
UI Layer (Presentation)
Implement the MVVM (Model-View-ViewModel) pattern to manage UI state and logic.
- Views: Write reusable, lean widgets. Restrict logic in Views to UI-specific operations (e.g., animations, layout constraints, simple routing). Pass all required data from the ViewModel.
- ViewModels: Manage UI state and handle user interactions. Extend
ChangeNotifier(or useListenable) to expose state. Expose immutable state snapshots to the View. Inject Repositories into ViewModels via the constructor.
Data Layer
Implement the Repository pattern to isolate data access logic and create a single source of truth.
- Services: Create stateless classes to wrap external APIs (HTTP clients, local databases, platform plugins). Return raw API models or
Resultwrappers. - Repositories: Consume one or more Services. Transform raw API models into clean Domain Models. Handle caching, offline synchronization, and retry logic. Expose Domain Models to ViewModels.
Logic Layer (Domain - Optional)
- Use Cases: Implement this layer only if the application contains complex business logic that clutters the ViewModel, or if logic must be reused across multiple ViewModels. Extract this logic into dedicated Use Case (interactor) classes that sit between ViewModels and Repositories.
Project Structure
Organize the codebase using a hybrid approach: group UI components by feature, and group Data/Domain components by type.
lib/
├── data/
│ ├── models/ # API models
│ ├── repositories/ # Repository implementations
│ └── services/ # API clients, local storage wrappers
├── domain/
│ ├── models/ # Clean domain models
│ └── use_cases/ # Optional business logic classes
└── ui/
├── core/ # Shared widgets, themes, typography
└── features/
└── [feature_name]/
├── view_models/
└── views/
Workflow: Implementing a New Feature
Follow this sequential workflow when adding a new feature to the application. Copy the checklist to track progress.
Task Progress
- Step 1: Define Domain Models. Create immutable data classes for the feature using
freezedorbuilt_value. - Step 2: Implement Services. Create or update Service classes to handle external API communication.
- Step 3: Implement Repositories. Create the Repository to consume Services and return Domain Models.
- Step 4: Apply Conditional Logic (Domain Layer).
- If the feature requires complex data transformation or cross-repository logic: Create a Use Case class.
- If the feature is a simple CRUD operation: Skip to Step 5.
- Step 5: Implement the ViewModel. Create the ViewModel extending
ChangeNotifier. Inject required Repositories/Use Cases. Expose immutable state and command methods. - Step 6: Implement the View. Create the UI widget. Use
ListenableBuilderorAnimatedBuilderto listen to ViewModel changes. - Step 7: Inject Dependencies. Register the new Service, Repository, and ViewModel in the dependency injection container (e.g.,
providerorget_it). - Step 8: Run Validator. Execute unit tests for the ViewModel and Repository.
- Feedback Loop: Run tests -> Review failures -> Fix logic -> Re-run until passing.
Examples
Data Layer: Service and Repository
// 1. Service (Raw API interaction)
class ApiClient {
Future<UserApiModel> fetchUser(String id) async {
// HTTP GET implementation...
}
}
// 2. Repository (Single source of truth, returns Domain Model)
class UserRepository {
UserRepository({required ApiClient apiClient}) : _apiClient = apiClient;
final ApiClient _apiClient;
User? _cachedUser;
Future<User> getUser(String id) async {
if (_cachedUser != null) return _cachedUser!;
final apiModel = await _apiClient.fetchUser(id);
_cachedUser = User(id: apiModel.id, name: apiModel.fullName); // Transform to Domain Model
return _cachedUser!;
}
}
UI Layer: ViewModel and View
// 3. ViewModel (State management and presentation logic)
class ProfileViewModel extends ChangeNotifier {
ProfileViewModel({required UserRepository userRepository})
: _userRepository = userRepository;
final UserRepository _userRepository;
User? _user;
User? get user => _user;
bool _isLoading = false;
bool get isLoading => _isLoading;
Future<void> loadProfile(String id) async {
_isLoading = true;
notifyListeners();
try {
_user = await _userRepository.getUser(id);
} finally {
_isLoading = false;
notifyListeners();
}
}
}
// 4. View (Dumb UI component)
class ProfileView extends StatelessWidget {
const ProfileView({super.key, required this.viewModel});
final ProfileViewModel viewModel;
@override
Widget build(BuildContext context) {
return ListenableBuilder(
listenable: viewModel,
builder: (context, _) {
if (viewModel.isLoading) {
return const Center(child: CircularProgressIndicator());
}
final user = viewModel.user;
if (user == null) {
return const Center(child: Text('User not found'));
}
return Column(
children: [
Text(user.name),
ElevatedButton(
onPressed: () => viewModel.loadProfile(user.id),
child: const Text('Refresh'),
),
],
);
},
);
}
}
flutter의 다른 스킬
dart-modern-features
flutter
현대화를 위한 후보를 찾으려면:
official
dart-log-failure-parser
flutter
Dart 및 Flutter 테스트 로그에서 실패를 파싱합니다.
official
find-release
flutter
주어진 커밋이 포함된 가장 낮은 Dart 및 Flutter 릴리스를 찾는 스킬입니다. 사용자가 Flutter나 Dart에서 커밋이 언제 포함되었는지 물을 때 이 스킬을 사용하세요…
official
flutter-pr-checks-finder
flutter
Flutter PR에서 실패한 검사를 찾고 해당 LUCI 로그 URL을 찾습니다.
official
rebuilding-flutter-tool
flutter
Flutter 도구와 CLI를 재빌드합니다. 사용자가 Flutter 도구나 CLI를 컴파일, 업데이트, 재생성 또는 재빌드하도록 요청할 때 사용하세요.
official
upgrade-browser
flutter
Flutter Web Engine 및/또는 Framework 테스트에서 브라우저 버전(Chrome 또는 Firefox)을 업그레이드합니다. Chrome 또는 Firefox를 최신 버전으로 롤 또는 업그레이드하라는 요청을 받을 때 사용하세요.
official
create-catalog-item
flutter
사용자가 JSON 스키마 정의를 기반으로 새 CatalogItem, 데이터 클래스 및/또는 위젯 클래스를 생성하도록 요청할 때 이 스킬을 사용하세요.
official
genui-helper
flutter
이 스킬은 genui 저장소에 특화된 워크플로우와 모범 사례를 제공합니다.
official