搜尋套件、儲存庫與 APP 分類

套件詳情v5.2.0

depend

depend 簡化了 Flutter 應用中的依賴管理,提供在組件樹中輕鬆初始化和存取服務的功能。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1180
喜歡數
10
Pub 分數
160

安裝套件

flutter pub add depend

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

depend 是一個輕量級的 Flutter 依賴注入套件,可簡化在整個 widget 樹中服務的初始化與存取。它讓開發者能以最少的樣板程式碼註冊與取得相依性,促進乾淨架構並簡化測試。此套件支援所有主要平台——Android、iOS、Web、macOS、Windows 與 Linux,並可透過簡單的 pub add 命令無縫整合至現有的 Flutter 專案中。藉由直覺式的 API,depend 提升程式碼的可維護性,減少對全域變數或上下文密集型模式的依賴。

適用情境

  • 跨多個螢幕管理共用服務
  • 簡化單元測試和小部件測試
  • 取代複雜的提供者設定
  • 啟用模組化應用架構
  • 減少依賴設定中的樣板程式碼

主要特色

  • 輕鬆的服務註冊與取得
  • 跨平台支援
  • 最小化樣板程式碼
  • 內建生命週期管理
  • 支援單例與暫時實例

適合對象

  • 中小型Flutter應用
  • 尋求簡單依賴注入解決方案的團隊
  • 需要清晰關注點分離的專案
  • 開發者避免使用重量級狀態管理庫
  • 針對多個平台的應用程式

使用建議

  • 可能不適合高度複雜的依賴關係圖
  • 與完整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

所屬分類

原始碼儲存庫

使用此套件的專案

暫無相關資料。