Flutterando / modular

A smart project structure
https://pub.dev/packages/flutter_modular
Other
1.31k stars 254 forks source link

PositionalParam is required and replaceInstance in cascade #955

Open fernandomoraesvr opened 7 months ago

fernandomoraesvr commented 7 months ago

Describe the question So my company team is migrating from Modular v5 to Modular v6 and we faced two issues when migrating.

The first one is, PositionalParams is being instanced as required parameters, what is not true, is this a known limitation regarding runtime injection? If it is not, i would be pleasure to contribute and resolve this possible issue.

Example: Dio has BaseOptions PositionalParam, what makes the following code throw an exception:

i.addSingleton<Dio>(Dio.new);

That can be fixed by the following code:

i.addSingleton<Dio>(() => Dio(BaseOptions()));

Well, this behaviour is not the end of the world, but i guess it could be fixed by PositionalParam be instanced with isRequired = false (or not due to limitations).

The second issue is that replaceInstance in our case is not replacing any Bind inside other instances, example:

i.addSingleton<Dio>(() => Dio(BaseOptions()));
i.addSingleton<RequestService>(RequestService.new);
i.addSingleton<AnyRepository>(AnyRepository.new)

RequestService contains Dio, which only gets replaced if i use replaceInstance in RequestService, this behavior occur in AnyRepository (it has RequestService instance, so i have to replace AnyRepository with ReplaceService that already is an instance with mocked Dio).

Expected behavior I did not known if both provided examples are expected or not, but i expect that replaceInstance replaces instances in cascade for any bind in Modular and define PositionalParam as not required.

Screenshots Since the code is from my company, i was not allowed to provide screenshots from our code, but i would be happy to help with more examples if needed.

fernandomoraesvr commented 7 months ago

Well, both of my issues are in auto_injector, but i'll keep this issue open and expect some answer before opening an issue in auto_injector repository.

I tried to change PositionalParam to not required in auto_injector and noticed that this will be propably not trivial. Yet, my main issue is regarding replaceInstance

DjordjeMancic97 commented 1 month ago

Any updates on this? @fernandomoraesvr were you able to solve the replaceInstance not working?