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

How to use scopes and environment filters? #446

Open
dustincatap opened this issue Apr 1, 2024 · 0 comments
Open

How to use scopes and environment filters? #446

dustincatap opened this issue Apr 1, 2024 · 0 comments

Comments

@dustincatap
Copy link

I am trying to register my scope first before calling $register and passing my custom EnvironmentFilter. But my dependencies annotated with my custom Environment are not registered.

@InjectableInit(
  initializerName: r'$register',
  preferRelativeImports: false,
  asExtension: false,
)
abstract final class ServiceLocator {
  static final GetIt instance = GetIt.instance..allowReassignment = true;

  static Future<void> registerDependencies() async {
    // We need to register forground services first
    initForegroundServiceScope(instance);

    // We need to manually override the pre-registered `EnvironmentFilter` with our new filter
    final environments = await _getEnvironments();
    final environmentFilter = NoEnvOrContainsAny(environments);
    instance.registerSingleton<EnvironmentFilter>(environmentFilter, instanceName: kEnvironmentsFilterName);

    await $register(instance, environmentFilter: environmentFilter);
  }

  static Future<Set<String>> _getEnvironments() async {
    // return custom environments based on current config...
  }
}

Upon checking, it seems that when registering a scope first, it registers a EnvironmentFilter with a instanceName using kEnvironmentsFilterName from get_it_helper.dart. This overrides the custom EnvironmentFilter I used in my $register. As a workaround I re-register my custom EnvironmentFilter.

Is this the expected behavior?

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

1 participant