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

Issue with Auto-generated Code Using @injectableInit - Bad State Error with GetIt Dependency Injection #466

Open
ShiyanaSam opened this issue May 16, 2024 · 8 comments

Comments

@ShiyanaSam
Copy link

After upgrading to Flutter 3.22.0, injectable auto generated code gives following exception

[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: GetIt: Object/factory with type BuildConfig is not registered inside GetIt.
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;

#injection.dart file
import 'package:get_it/get_it.dart';
import 'package:injectable/injectable.dart';
import 'package:testapp/injection.config.dart';

final GetIt getIt = GetIt.instance;

@InjectableInit()
void configureInjection(String env) {
getIt.init(environment: env);

}

..............................

@Injectable(as: BuildConfig, env: [Environment.dev])
class BuildConfigDev extends BuildConfig {
@OverRide
String get baseUrl => secrets.baseDevUrl;

@OverRide
Future configure(Locale? locale) async => runApp(
MyApp(
locale: locale,
),
);
}

...............................

#injectable_api_module.dart file

@module
abstract class InjectableApiModule {
@singleton
UserApi get userApi => UserApi(
NetworkService.dio,
baseUrl: getIt().baseUrl, //this is where the exception occurs when running the app
);

}

@KarinBerg
Copy link

KarinBerg commented May 30, 2024

@ShiyanaSam Which version of "injectable_generator" do you use? If it is 2.6.1 try downgrading to 2.6.0 and tell me if it is working again.

I had a similar issue with the exception:

StateError (Bad state: GetIt: Object/factory with type AppEnv 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?))

My workaround solution:
I only changed the version of injectable_generator from 2.6.1 back to 2.6.0 and it is working again.

Source of the issue:
For me it seems that the PR #438 (the only change in version 2.6.1) seems to cause the issue. My order of the registered classes changed which leads to a missing object state error from above for the type "AppEnv" which is now registered after the first use.

With v2.6.0 it is:

_i1.GetIt init({
    String? environment,
    _i2.EnvironmentFilter? environmentFilter,
  }) {
    final gh = _i2.GetItHelper(
      this,
      environment,
      environmentFilter,
    );
    final configModule = _$ConfigModule();
    final serviceModule = _$ServiceModule();
    gh.singleton<_i3.AppEnv>(
      () => configModule.prodEnv,
      registerFor: {_prod},
    );
    gh.singleton<_i3.AppEnv>(
      () => configModule.devEnv,
      registerFor: {_dev},
    );
    gh.singleton< ...
    gh.singleton< ...
    ...
}

In v2.6.1 it is:

_i1.GetIt init({
    String? environment,
    _i2.EnvironmentFilter? environmentFilter,
  }) {
    final gh = _i2.GetItHelper(
      this,
      environment,
      environmentFilter,
    );
    final configModule = _$ConfigModule();
    final serviceModule = _$ServiceModule();
    ...
    gh.singleton< ...
    gh.singleton< ...
    ...
    gh.singleton<_i3.AppEnv>(
      () => configModule.devEnv,
      registerFor: {_dev},
    );
    ...
   gh.singleton< ...
   gh.singleton< ...
   gh.singleton< ...
   gh.singleton< ...
   ...
   gh.singleton<_i3.AppEnv>(
      () => configModule.prodEnv,
      registerFor: {_prod},
    );

@Adam-Langley: Do you have any idea what can cause this wrong registration order after your PR? It could be related to the "registerFor" argument. But I'm pretty new to this code generating stuff 🙂.

@Adam-Langley
Copy link
Contributor

Hi @KarinBerg, @ShiyanaSam would either of you be able to provide a minimal reproduction project?

Thanks,

@KarinBerg
Copy link

I check if I can create a small sample with my problem.

@KarinBerg
Copy link

ok, I managed to create a small example. You can download the project here: Link.

This example contains two launch configurations which starting the app via two different main functions and in theory with different environment configurations. One to start the app with "PROD" environment and one for the "DEV" (notice that this is just simulated here. I do not pass any config file in this example). The example uses flutter 3.19.3 in case this is relevant. You can use the generateApp.sh script to fetch all dependencies and run the builder like I do. I used an iOS simulator as a target device.

To get the StateError "Bad state: GetIt: Object/factory with type AppEnv is not registered inside GetIt." you have to use the version 2.6.1 of the injectable_generator and start the app via the main_prod.dart launch configuration. The StateError is not thrown if you start the app via the main_dev.dart launch configuration.

The StateError is also not thrown if you use the version 2.6.0 of the injectable_generator and starting the app via any launch configuration.

What I also notice while creating this example is, if I move the file "app_env.dart" from "lib/core" to "lib", using injectable_generator:2.6.1 and rerun the builder then the StateError is also not thrown. Unfortunatelly this change does not fix the problem in my real flutter project. But I wanted to mention it. Maybe it is a hint for you :).

I hope this helps you to find the problem. I you need any info then please let me know.
Thanks for your time and help,
Karin

@KarinBerg
Copy link

@Adam-Langley Do you have some time to check my example project and the issue?

@gj-thaoht
Copy link

gj-thaoht commented Oct 24, 2024

Hi @KarinBerg, @ShiyanaSam would either of you be able to provide a minimal reproduction project?

Thanks,

I find the topic 477 resolves issues the same.

@KarinBerg
Copy link

KarinBerg commented Oct 24, 2024

I can confirm that #477 is resolving my issue, too.

I updated my example project from above: Link

@KarinBerg
Copy link

@Milad-Akarie Would be awesome if you could review the #477 🤩

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

4 participants