2

I'm trying to register a singleton of one class which implements two interfaces. Is that possible? The generator states, that one interface is not a subtype of it's own.

abstract class IOne { ... }
abstract class ITwo { ... }

@module
abstract class RegisterMySingleton {
  @LazySingleton(as: IOne)
  IOne get one => getIt<MySingleton>();
  @LazySingleton(as: ITwo )
  ITwo get two => getIt<MySingleton>();
}

@lazySingleton
class MySingleton implements IOne, ITwo { ... }

Output of flutter pub run build_runner build --delete-conflicting-outputs

[IOne] is not a subtype of [IOne]
Matthias
  • 31
  • 2

1 Answers1

0

Maybe it's too late for you @Matthias, but maybe someone will find this helpful. I was able to solve the problem above like this (even it's architectonically disputable):

@module
abstract class AppModule {
  @preResolve
  Future<Repository> get repository => RepositoryImpl.init();

  @injectable
  MyServiceImpl get myService(Repository repository) => MyServiceImpl(repository);

  @injectable 
  One get one(MyServiceImpl myService) => myService;

  @injectable 
  Two get two(MyServiceImpl myService) => myService;
  
  @injectable 
  Three get three(MyServiceImpl myService) => myService;
}

PS: For clarity, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. DI does not ensure there is only one instance in the system. There is usually only one instance in the 'DI system'.

JiangHongTiao
  • 858
  • 2
  • 8
  • 29