fix: hot reload makes instance registered twice when using get_it package
Description
I have some problem when i registered object or instance using get_it, when server is hot reloaded get_it re-registered instance make server stop running
Steps To Reproduce
- Create custom entrypoint
- Register Instance on Get It
- Run server
- Do hot reload
Expected Behavior Instance registered only once
A clear and concise description of what you expected to happen.
Screenshots

My Get It

My Custom Entry Point

If applicable, add screenshots to help explain your problem.
Additional Context
Add any other context about the problem here.
Hi @ekokurniadi 👋 Thanks for opening an issue!
I’ll take a closer look at this shortly but out of curiosity was there any specific reason why you decided to include get_it instead of using the built in provider API for dependency injection?
Thanks! 🙏
Hi @felangel can I use custom dependency injection like injectable or get_it ?
Hi @felangel i will close this issue, Thanks for your response
Hi @felangel i will close this issue, Thanks for your response
Did you manage to resolve the issue?
I had same situation. Actually I'm using the built in DI for routes but I also have some job runners and those don't have a "api call", just a cron to run after start.
The "problem" is about hot reload, after changes, reload everything, even the "init()". A solution would be dart frog offers a place to "init" server and it should not be called on hot reload, only once on start.
For now, I figured out 2 workarounds options to handle this for me.
- just a flag to check it
Future<void> init() async {
if (_didSetup) return;
_didSetup = true;
...
- use the get it built in verifier:
if (!GetIt.I.isRegistered<MySingleton>()) {
GetIt.I.registerLazySingleton<MySingleton>();
}
I'm going to re-open this since it sounds like something that should be supported by Dart Frog's hot reload.
I've encountered this problem recently, and as @GabrielRozendo stated, it only occurs after hot reloads. As I don't mind about my singletons being instantiated again, I just reset them all after each hot reload :
Future<HttpServer> run(Handler handler, InternetAddress ip, int port) async {
await GetIt.I.reset();
configureDependencies();
return serve(handler, ip, port);
}
I'm using Injectable instead of the built in provider API because it simplifies working in multiple environments, and also because of periodic jobs runners that don't have access to a context to read from.
related to https://github.com/fluttercommunity/get_it/issues/166
I had same situation. Actually I'm using the built in DI for routes but I also have some job runners and those don't have a "api call", just a cron to run after start.
The "problem" is about hot reload, after changes, reload everything, even the "init()". A solution would be dart frog offers a place to "init" server and it should not be called on hot reload, only once on start.
For now, I figured out 2 workarounds options to handle this for me.
- just a flag to check it
Future<void> init() async { if (_didSetup) return; _didSetup = true; ...
- use the get it built in verifier:
if (!GetIt.I.isRegistered<MySingleton>()) { GetIt.I.registerLazySingleton<MySingleton>(); }
I used the same get it verifier as you and it did the trick.
Closing for now since this does not appear to be directly related to Dart Frog. As folks have mentioned, you can check if an object has been registered already to prevent re-registering.