Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@lazySingleton creates at start when @PostConstruct(preResolve: true) #486

Open
deandreamatias-worldline opened this issue Jan 7, 2025 · 4 comments

Comments

@deandreamatias-worldline
Copy link

deandreamatias-worldline commented Jan 7, 2025

Problem

I don't know if I'm misunderstanding PostConstruct proposal, but I think it doesn't work as expected.

I have a use_case file which is lazySingleton. Inside this, a method (call) that needs to be initialized before using the use_case.

Current behavior:

The singleton is being initialized when I run the application and not when it is called for the first time (at home).

Expected behavior:

I would like it to be initialized only when I call it at home.

Other info

If I remove the PostConstruct, it works.

Setup

injectable version: 2.5.0
get_it version: 8.0.2

Code:

import 'package:injectable/injectable.dart';

@lazySingleton
class GetFacilitiesUseCase {
  GetFacilitiesUseCase();

  final List<Facility> _facilitiesWithVisitedState = [];

  Future<Facility> getFacility({required int nid}) async {
   ...
  }

  @PostConstruct(preResolve: true)
  Future<List<Facility>> call() async {
   ...
  }

  @disposeMethod
  void dispose() {
    ...
  }
}

Generated code:

    await gh.lazySingletonAsync<_i39.GetFacilitiesUseCase>(
      () {
        final i = _i39.GetFacilitiesUseCase();
        return i.call().then((_) => i);
      },
      preResolve: true,
      dispose: (i) => i.dispose(),
    );
@Milad-Akarie
Copy link
Owner

that makes sense, because the instance is resolved at setup phase, try disabling preResolve

@deandreamatias-worldline
Copy link
Author

Ok. In next days, I will test this setup and send here the conclusion

@deandreamatias-worldline
Copy link
Author

deandreamatias-worldline commented Jan 16, 2025

@Milad-Akarie I changed this and had other problem. Other injectables that need required parameter, receive nullable parameters. In this generate code, facilityNid is nullable:

    gh.factoryParamAsync<_i327.FacilityMapDetailViewModel, int, dynamic>((
      facilityNid,
      _,
    ) async =>
        _i327.FacilityMapDetailViewModel(
          env: gh<_i679.Env>(),
          facilitiesUseCase: await getAsync<_i39.GetFacilitiesUseCase>(),
          facilityNid: facilityNid,
        ));

This is because this package code, where P1 and P2 are nullable:

FactoryFuncParamAsync<T, P1?, P2?> factoryFunc, {

My current solution was:

  1. Remove PostConstructor
  2. Call GetFacilitiesUseCase after dependency injection init (native splash) but before of first effective use (FacilitiesScreen). The result: I called on my Flutter custom splash

Desired behavior:
When using PostConstructor(preResolve: true) in a lazySingleton method, the indicated method is loaded just before the use case is used.
Current behavior:
The method and the singleton are loaded when the app is opened (native splash)

@Milad-Akarie
Copy link
Owner

Where do you expect the param for factory to come from? Normally you would pass it as you create the instance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants