Milad-Akarie / injectable

Code Generator for get_it
MIT License
548 stars 143 forks source link

Error binding an abstract class to multiple implementations #400

Open ramirezsebas opened 1 year ago

ramirezsebas commented 1 year ago

This is my flutter doctor

image

I am using:

I've designed an abstract class that serves as a repository within my app. To implement this repository, I've created a concrete implementation that adheres to the methods defined in the abstract class, ensuring a consistent contract for data operations. Additionally, I'd like to mention that I am injecting the repository into my Cubit, where the Cubit acts as a state management solution.

abstract class MyBaseRepository{}

@Injectable(as: MyBaseRepository) class MyRepository implements MyBaseRepository{}

Everything works perfectly, but when I add @Named("api") @Named("Api") @Injectable(as: MyBaseRepository) class MyRepository implements MyBaseRepository{}

class MyCubit extends Cubit { final MyRepository _westernUnionRepository;

MyCubit(@Named('Api') this._westernUnionRepository) : super(MyInitial()); }

BlocProvider( create: (context) => MyCubit( getItInjector(), ), child: const MyView(), );

I get the following error: Bad state: GetIt: Object/factory with type MyRepository is not registered inside GetIt. (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance; Did you forget to register it?)

I have followed the instructions in the documentation

image
Milad-Akarie commented 1 year ago

@ramirezsebas I believe the issue here has to do with the fact that you're injecting the repository manually, and you're calling getit without giving it the name.

why not mark your block as injectable as well and have injectable build the whole thing?

github-actions[bot] commented 11 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions