
architecture-recommendation
by lwj1994
pure & lightweight state management library for Flutter
SKILL.md
name: Architecture Recommendation description: Guidelines for structuring applications using the view_model package, promoting a simplified, mixin-based architecture.
name: Architecture Recommendation description: Guidelines for structuring applications using the view_model package, promoting a simplified, mixin-based architecture.
Architecture Recommendation
The view_model package promotes a simplified architecture where everything can be a ViewModel. By leveraging the ViewModel and Vef mixins, you can eliminate complex layering often found in other architectures.
Core Principles
- Universal Component: Widgets, Repositories, Services, and Managers can all be
ViewModels. - No Context Hell: Access state and logic anywhere without passing
BuildContext. - Composition over Inheritance: Use mixins (
with ViewModel,with Vef) to add capabilities.
1. Universal Component (with ViewModel)
Transform any class into a capable component by mixing in ViewModel. This gives it access to vef for dependency injection and lifecycle management.
class UserRepository with ViewModel {
Future<User> fetchUser() async {
// Access other ViewModels seamlessly via 'vef'
final token = vef.read(authProvider).token;
return api.get(token);
}
}
2. Dependency Injection (VM ↔ VM)
ViewModels interpret dependencies by simply reading other providers.
class CartViewModel with ViewModel {
void checkout() {
// 1. Get UserViewModel instance
final userVM = vef.read(userProvider);
// 2. Use it directly
if (userVM.isLoggedIn) {
processOrder();
}
}
}
3. Reactive Logic (Internal Listening)
ViewModels can listen to other ViewModels (listenState) and react to changes automatically, keeping business logic encapsulated.
class ChatViewModel with ViewModel {
ChatViewModel() {
// Automatically react when AuthProvider's state changes
listenState(authProvider, (previous, next) {
if (next.isLoggedOut) {
clearMessages();
}
});
}
}
4. Initialization & Tasks (with Vef)
For logic that doesn't need to hold state or be a ViewModel (like startup tasks), use with Vef to gain access to the provider system.
class AppInitializer with Vef {
Future<void> init() async {
// Read and initialize ViewModels sequentially or parallelly
await vef.read(configProvider).fetch();
await vef.read(authProvider).check();
}
}
// Usage in main
void main() {
AppInitializer().init();
runApp(MyApp());
}
5. Singletons & Global State
For services that must remain alive throughout the app lifecycle (Auth, Settings), use aliveForever: true.
final authProvider = ViewModelProvider(
builder: () => AuthViewModel(),
key: (_) => 'auth_global', // Optional: simpler debugging
aliveForever: true, // Prevents auto-disposal
);
Summary
| Component | Mixin | Usage |
|---|---|---|
| Widget | ViewModelStateMixin | UI rendering, binding to ViewModels |
| Business Logic | ViewModel | State management, simple logic |
| Service/Repo | ViewModel | Data fetching, complex logic, dependencies |
| TaskScript | Vef | Scripts, initialization, one-off tasks |
Score
Total Score
Based on repository quality metrics
SKILL.mdファイルが含まれている
ライセンスが設定されている
100文字以上の説明がある
GitHub Stars 100以上
1ヶ月以内に更新
10回以上フォークされている
オープンIssueが50未満
プログラミング言語が設定されている
1つ以上のタグが設定されている
Reviews
Reviews coming soon
