Skip to content

Singletons not being instantiated properly #128

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

Closed
redsuperbat opened this issue Nov 15, 2020 · 2 comments
Closed

Singletons not being instantiated properly #128

redsuperbat opened this issue Nov 15, 2020 · 2 comments

Comments

@redsuperbat
Copy link

redsuperbat commented Nov 15, 2020

This is how i setup my singletons in the main function.

void main() {
  // setup();
  GetIt.I.registerSingleton(() => ThemeService());
  GetIt.I.registerSingleton(() => ImageService());
  GetIt.I.registerSingleton(() => AuthService());
  GetIt.I.registerSingleton(() => WineService());
  GetIt.I.registerSingleton(() => CellarService());
  runApp(RestartWidget(child: MyApp()));
}

This is the error stack

I/flutter ( 5797): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 5797): The following assertion was thrown building FutureBuilder<void>(dirty, state:
I/flutter ( 5797): _FutureBuilderState<void>#ae63d):
I/flutter ( 5797): Object/factory with  type ThemeService is not registered inside GetIt.
I/flutter ( 5797): (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
I/flutter ( 5797): Did you forget to register it?)
I/flutter ( 5797): 'package:get_it/get_it_impl.dart':
I/flutter ( 5797): Failed assertion: line 298 pos 9: 'instanceFactory != null'
I/flutter ( 5797):
I/flutter ( 5797): The relevant error-causing widget was:
I/flutter ( 5797):   FutureBuilder<void>
package:wine_cellar/main.dart:28
I/flutter ( 5797):
I/flutter ( 5797): When the exception was thrown, this was the stack:

Then in app I'm accessing the themeservice however this is what throws the error. The services should have been instantiated already and should not throw the error. Anyone knows why I would really like to know! =)

class App extends StatelessWidget {
  final themeService = GetIt.I<ThemeService>();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
      initialData: WineCellarThemes.light,
      stream: themeService.theme$,
      builder: (context, snapshot) => MaterialApp(
        theme: snapshot.data,
        onGenerateRoute: (settings) => Router.generateRoute(settings),
        home: LandingPage(),
      ),
    );
  }
}
@redsuperbat
Copy link
Author

The solution was my syntax before the runApp function. Should be

GetIt.I.registerSingleton<ServiceType>(ServiceType())

@escamoteur
Copy link
Collaborator

Strange, my reply was not received by Github. But you are exactly right. Always provide the Generic type. then you would have seen a type error in your first approach.

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