パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v5.2.0

depend

depend は Flutter アプリにおける依存関係の管理を簡素化し、ウィジェットツリー全体でサービスの初期化とアクセスを容易にします。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
1180
いいね数
10
Pub ポイント
160

パッケージをインストール

flutter pub add depend

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

depend は、Flutter用の軽量な依存関係注入パッケージであり、ウィジェットツリー全体でのサービスの初期化とアクセスを簡素化します。最小限のボイラープレートで依存関係を登録・取得でき、クリーンアーキテクチャの実現とテストの容易さを促進します。このパッケージは Android、iOS、Web、macOS、Windows、Linux のすべての主要プラットフォームをサポートしており、単純な pub add コマンドで既存の Flutter プロジェクトにシームレスに統合できます。直感的な API を備え、コードの保守性を高め、グローバル変数やコンテキスト依存のパターンへの依存を減らします。

用途

  • 複数のスクリーンにわたる共有サービスの管理
  • ユニットおよびウィジェットのテストを簡素化する
  • 複雑なプロバイダーセットアップの置き換え
  • モジュールアーキテクチャアプリの有効化
  • 依存関係のセットアップにおけるボイラープレートの削減

主な機能

  • 簡単なサービスの登録と取得
  • クロスプラットフォーム対応
  • 最小限のボイラープレート
  • 組み込みライフサイクル管理
  • シングルトンおよび一時インスタンスの両方をサポートしています

適している対象

  • 小規模から中規模のFlutterアプリケーション
  • シンプルなDIソリューションを求めるチーム
  • 関心事の明確な分離を必要とするプロジェクト
  • 重い状態管理ライブラリを避ける開発者
  • 複数のプラットフォームをターゲットにするアプリ

注意事項

  • 非常に複雑な依存関係グラフには適していない可能性があります
  • 完全なDIフレームワークと比較して、高度な機能が限られている
  • 単純な依存関係のニーズに最適
  • 広範なランタイム構成を必要とするアプリには適していません
  • 大規模なアプリケーションには慎重な計画が必要です

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Logo Frame

depend is a library for dependency management in Flutter applications. It provides a convenient way to initialize and access services and repositories via InheritedWidget.


Features 🚀

  • Dependency Initialization: Prepare all dependencies before the app launches.
  • Global Access: Access dependencies from anywhere in the widget tree.
  • Parent Dependencies Support: Easily create nested or connected dependencies.
  • Ease of Use: Integrate the library into existing code with minimal changes.

Table of Contents


Installation

Add the library to the pubspec.yaml of your project:

dependencies:
  depend: ^latest_version

Install the dependencies:

flutter pub get

Usage Examples

Example 1: Simple Initialization
Step 1: Define the Dependency

Create a class that extends DependencyContainer and initialize your dependencies:

class RootContainer extends DependencyContainer {
  final ApiService apiService;

  RootContainer({required this.apiService});


  void dispose() {
    apiService.dispose();
  }
}
Step 2: Define the DependencyFactory

Create a class that extends DependencyContainer and initialize your dependencies:

class RootDependencyFactory extends DependencyFactory<RootContainer> {
  
  Future<RootContainer> create() async {
    return RootContainer(
      apiService: await ApiService.initialize(),
    );
  }
  
  // or

  RootContainer create() {
     return RootContainer(
        apiService: ApiService.initialize(),
     );
  }
}
Step 3: Use DependencyScope

Wrap your app in a DependencyScope to provide dependencies:

void main() {
  runApp(
    DependencyScope<RootContainer, RootFactory>(
       factory: RootFactory(), 
       placeholder: const Center(child: CircularProgressIndicator()), 
       builder: (BuildContext context) => const MyApp(),
    ),
  );
}
Step 4: Access the Dependency in a Widget

You can now access the dependency using DependencyProvider anywhere in the widget tree:

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final apiService = DependencyProvider.of<RootContainer>(context).apiService;

    return FutureBuilder(
      future: apiService.getData(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return const CircularProgressIndicator();
        } else if (snapshot.hasError) {
          return Text('Error: ${snapshot.error}');
        }

        return Text('Data: ${snapshot.data}');
      },
    );
  }
}

Example 2: DependencyProvider
final RootContainer dep = await RootFactory().create();

DependencyProvider<RootContainer>(
  dependency: dep,
  builder: () => YourWidget();
  // or
  child: YourWidget()
)

class YourWidget extends StatelessWidget {
  @override
  Widget build(BuildContext) {
    root = DependencyProvider.of<RootContainer>(context);
    ...
  }
}
Example 3: DependencyScope
DependencyScope<RootContainer, RootFactory>(
      factory: RootFactory(),
      builder: (BuildContext context) => Text('Inject'),
      placeholder: Text('Placeholder'),
      errorBuilder: (Object? error) => Text('Error'),
    ),

Example 4: Lazy Initialization

The library provides LazyGet and LazyFutureGet classes for lazy initialization of services. Dependencies are created only when they are first accessed, which improves application startup time.

Using LazyGet for Synchronous Dependencies
class RootContainer extends DependencyContainer {
  // Service will be created only when accessed for the first time
  final LazyGet<ApiService> apiService;
  final LazyGet<DatabaseService> database;
}

class RootDependencyFactory extends DependencyFactory<RootContainer> {
  RootContainer create() {
     return RootContainer(
        apiService: lazyGet(ApiService.initialize),
        database: lazyGet(DatabaseService.initialize),
     );
  }
}

// Usage in widgets
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final container = DependencyProvider.of<RootContainer>(context);
    
    // ApiService is created only at this moment
    final api = container.apiService();
    
    return Text('Service initialized: ${api}'); // Instance: ApiService
  }
}
Using LazyFutureGet for Asynchronous Dependencies
class RootContainer extends DependencyContainer {
  final LazyFutureGet<DatabaseService> database;
      
  final LazyFutureGet<AuthService> authService;
}

class RootDependencyFactory extends DependencyFactory<RootContainer> {
  RootContainer create() {
     return RootContainer(
        apiService: lazyFutureGet(ApiService.initialize),
        database: lazyFutureGet(DatabaseService.initialize),
     );
  }
}

// Usage in widgets
class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final container = DependencyProvider.of<RootContainer>(context);
    
    return FutureBuilder<DatabaseService>(
      // Database is initialized only when this widget is built
      future: container.database(),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.waiting) {
          return CircularProgressIndicator();
        }
        
        return Text('Database ready: ${snapshot.data?.isConnected}');
      },
    );
  }
}

Benefits of Lazy Initialization:

  • Faster App Startup: Dependencies are created only when needed
  • Memory Optimization: Unused services don't consume memory
  • Flexible Initialization: You can control when heavy operations are performed
  • Simple API: Easy to use with both sync and async dependencies

Codecov

Codecov

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。