GetIt/Injectable missing injectable decorator on abstract class?

Viewed 1297

I'm using GetIt and Injectable, and I have some trouble with 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. 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:

[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. Then launch the app and click on the FAB at the bottom right of the screen to see the error in the app.

2 Answers

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

Only non-nullable constructor object arguments will generate, even if the underlying class is marked as @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.

Related