スキル一覧に戻る
lwj1994

architecture-recommendation

by lwj1994

pure & lightweight state management library for Flutter

20🍴 1📅 2026年1月24日
GitHubで見るManusで実行

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

  1. Universal Component: Widgets, Repositories, Services, and Managers can all be ViewModels.
  2. No Context Hell: Access state and logic anywhere without passing BuildContext.
  3. 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

ComponentMixinUsage
WidgetViewModelStateMixinUI rendering, binding to ViewModels
Business LogicViewModelState management, simple logic
Service/RepoViewModelData fetching, complex logic, dependencies
TaskScriptVefScripts, initialization, one-off tasks

スコア

総合スコア

65/100

リポジトリの品質指標に基づく評価

SKILL.md

SKILL.mdファイルが含まれている

+20
LICENSE

ライセンスが設定されている

+10
説明文

100文字以上の説明がある

0/10
人気

GitHub Stars 100以上

0/15
最近の活動

1ヶ月以内に更新

+10
フォーク

10回以上フォークされている

0/5
Issue管理

オープンIssueが50未満

+5
言語

プログラミング言語が設定されている

+5
タグ

1つ以上のタグが設定されている

+5

レビュー

💬

レビュー機能は近日公開予定です