繁体   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