[英]How to get Injectable to generate dependency injection for third party Abstract class thats using factory constructors
我正在嘗試使用 package Injectable注冊下面的 class 。
import 'dart:io';
import 'package:injectable/injectable.dart';
@injectable
class IWantToBeRegistered {
final File file;
IWantToBeRegistered({
required this.file,
});
}
我得到以下回復:
[WARNING] injectable_generator:injectable_config_builder on bin/service_locator.dart:
Missing dependencies in your_project/bin/service_locator.dart
[MachineCodeWriter] depends on unregistered type [File] from dart:io
Did you forget to annotate the above class(s) or their implementation with @injectable?
or add the right environment keys?
我曾嘗試將File
注冊為第三方類型,但沒有成功。 在Injectable 的文檔中確實說可以在配置 function 中手動注冊它,但是我找不到一個工作示例並且我自己未能成功實現它。
該項目是 Dart 中的命令行應用程序,我正在使用 SDK 和 package 版本:
Dart SDK version: 2.15.1 (stable)
get_it: ^7.2.0
injectable: ^1.5.0
injectable_generator: ^1.5.0
謝謝你的幫助!
為此,您可以使用RegisterModule
。
您可以從這里檢查如何使用 RegisterModule 進行注射
@module
abstract class RegisterModule {
YourThirdParty getThirdPartyObjectInstance(TypeOfThirdParty typedObject, @factoryParam String url) => YourThirdParty(obj, url);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.