-
Notifications
You must be signed in to change notification settings - Fork 143
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
Comments
@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:
My workaround solution: Source of the issue: With v2.6.0 it is:
In v2.6.1 it is:
@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 🙂. |
Hi @KarinBerg, @ShiyanaSam would either of you be able to provide a minimal reproduction project? Thanks, |
I check if I can create a small sample with my problem. |
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. |
@Adam-Langley Do you have some time to check my example project and the issue? |
I find the topic 477 resolves issues the same. |
@Milad-Akarie Would be awesome if you could review the #477 🤩 |
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
);
}
The text was updated successfully, but these errors were encountered: