簡體   English   中英

GetIt/Injectable 在抽象 class 上缺少可注入裝飾器?

[英]GetIt/Injectable missing injectable decorator on abstract class?

我正在使用GetItInjectable ,但在使用 GetIt 時遇到了一些問題。

看來我沒有注冊我的DiscordRepository class,但我不能用@injectable 裝飾器注冊它,因為它是一個抽象的 class。 在這種情況下我該怎么辦?

我像這樣初始化我的依賴注入:


final getFromDependencyGraph = GetIt.instance;

@injectableInit
Future<void> setupDependencyInjection() async {
  await $initGetIt(getFromDependencyGraph);
}

flutter pub run build_runner build給了我這個 output:

[WARNING] injectable_generator:injectable_config_builder on lib/misc/dependencies.dart:
Missing dependencies in discordlogin/misc/dependencies.dart

[AuthenticationService] depends on unregistered type [DiscordRepository] from package:discordlogin/data/repository/discord_repository.dart

Did you forget to annotate the above class(s) or their implementation with @injectable?

我的完整代碼在這里: https://github.com/BLKKKBVSIK/DiscordLogin為 web 構建它。 然后啟動應用程序並單擊屏幕右下角的 FAB 以查看應用程序中的錯誤。

對我來說,問題是在 flutter 2.0 之后,該項目是由一些插件自動更改為 null-safety,它提供了不必要的字段,即 null-safety(?),在我清理之后警告消失了

只有不可為空的構造函數 object arguments 會生成,即使底層的 class 被標記為@injectable。

解決方案是刪除可為空的? 指令並替換為必需的關鍵字,如下所示:

改變:

class SomeSingleton{
 
final SomeInjectable? injectable;

SomeSingleton({this.injectable])
}

至:

class SomeSingleton{
 
final SomeInjectable injectable;

SomeSingleton({required this.injectable])
}

現在上面的代碼片段的 SomeInjectable object 不能是 null ,因此生成器會相應地注入它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM