简体   繁体   English

GetIt/Injectable 在抽象 class 上缺少可注入装饰器?

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

I'm using GetIt and Injectable , and I have some trouble with GetIt.我正在使用GetItInjectable ,但在使用 GetIt 时遇到了一些问题。

It seems that I'm not registering my DiscordRepository class, but I can't registering it with a @injectable decorator since it's an abstract class.看来我没有注册我的DiscordRepository class,但我不能用@injectable 装饰器注册它,因为它是一个抽象的 class。 What should I do in this case?在这种情况下我该怎么办?

I initialised my dependency injection like so:我像这样初始化我的依赖注入:


final getFromDependencyGraph = GetIt.instance;

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

The flutter pub run build_runner build give me this output: 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?

My full code is here: https://github.com/BLKKKBVSIK/DiscordLogin Build it for web.我的完整代码在这里: https://github.com/BLKKKBVSIK/DiscordLogin为 web 构建它。 Then launch the app and click on the FAB at the bottom right of the screen to see the error in the app.然后启动应用程序并单击屏幕右下角的 FAB 以查看应用程序中的错误。

For me it was problem that after flutter 2.0, the project was a by some plugin automatic changed into null-safety which provides unnecessary fields which is null-safety(?), after I clean this up the warnings disapear对我来说,问题是在 flutter 2.0 之后,该项目是由一些插件自动更改为 null-safety,它提供了不必要的字段,即 null-safety(?),在我清理之后警告消失了

Only non-nullable constructor object arguments will generate, even if the underlying class is marked as @injectable.只有不可为空的构造函数 object arguments 会生成,即使底层的 class 被标记为@injectable。

The solution is to remove the nullable?解决方案是删除可为空的? directive and replace with a required keyword like so:指令并替换为必需的关键字,如下所示:

change:改变:

class SomeSingleton{
 
final SomeInjectable? injectable;

SomeSingleton({this.injectable])
}

to:至:

class SomeSingleton{
 
final SomeInjectable injectable;

SomeSingleton({required this.injectable])
}

Now the above snippet's SomeInjectable object cannot be null and therefore the generator will inject it accordingly.现在上面的代码片段的 SomeInjectable object 不能是 null ,因此生成器会相应地注入它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM