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

套件詳情v4.0.0

injector

injector 是一個簡單易用且輕量級的 Dart 依賴注入庫

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
92.8萬
喜歡數
63
Pub 分數
160

安裝套件

flutter pub add injector

套件介紹

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

Injector 是一個輕量級且易於使用的 Dart 依賴注入程式庫,專為簡化 Flutter 應用程式的相依性管理而設計。它提供一個乾淨、最小化的 API 來註冊和解析相依性,支援單例與暫時實例。透過與 Flutter 專案的無縫整合,它提升了程式碼的模組化與可測試性。此程式庫非常適合尋求比更複雜 DI 解決方案更簡單替代方案,又不犧牲功能的開發者。

適用情境

  • Flutter 應用中的簡單依賴管理
  • 使用模擬依賴項進行測試
  • 模組化應用架構
  • 減少服務實例化中的樣板程式碼
  • 簡化狀態管理設定
  • 在組件之間實現鬆耦合

主要特色

  • 簡潔直覺的 API
  • 對單例和暫態綁定的支援
  • 與 Flutter 的輕鬆整合
  • 內建的建構函數注入支援
  • 輕量級佔用
  • 完整的文件

適合對象

  • 初學者到中級 Flutter 開發者
  • 小型到中型應用
  • 優先考慮簡單性而非進階功能的專案
  • 尋求快速依賴注入實作的團隊
  • 需要可測試且可維護程式碼的應用程式

使用建議

  • 與功能齊全的 DI 庫相比,進階功能有限
  • 不太適合具有複雜依賴關係的超大規模應用程式
  • 不支援基於樹狀的範圍或階層式相依性解析
  • 在複雜場景中可能需要手動管理生命週期

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Injector 💉 Build Status

Injector is a simple dependency injection lib for Dart.

It does not replace a complex dependency injection framework like Dagger, but it provides the basics that most apps need. Feature requests are welcomed!

Internally the injector is a singleton that stores instances and builders in a Map.

Use registerDependency<Type>() to register the dependency. Never ever try to register or get dependencies without the generic type! Dart allows it, but we don't ;)

All instances are lazy loaded, meaning that at the time you request the dependency the instance is created.

Get your dependency using get<Type>().

Usage

A simple usage example:

// Register a dependency (Every time a new instance)
injector.registerDependency<Car>(() {
      var engine = injector.get<Engine>();
      var fuel = injector.get<Fuel>();
      var driver = injector.get<Driver>();
      
      return CarImpl(engine,fuel,driver);
    });
    
// Register a singleton
injector.registerSingleton<Car>(() {
          var engine = injector.get<Engine>();
          var fuel = injector.get<Fuel>();
          var driver = injector.get<Driver>();
          
          return CarImpl(engine,fuel,driver);
        });
        
 // Use custom Factories by extending [Factory].
 injector.register(CustomFactory(() => Engine()));
    
// Register your dependencies / singletons in the  __main.dart__ file

Notice

If you have an issue with getting dependencies from the injector, check that imports of your dependencies classes are absolute.

Good: import 'package:<project_name>/some/package/<file_name>.dart';

Bad: import '../../some/package/<file_name>.dart';

Features and bugs

Please file feature requests and bugs at the issue tracker.

所屬分類

原始碼儲存庫

使用此套件的專案