Skip to content

GetIt fails to register instances #81

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
danagbemava opened this issue May 23, 2020 · 4 comments
Closed

GetIt fails to register instances #81

danagbemava opened this issue May 23, 2020 · 4 comments

Comments

@danagbemava
Copy link

danagbemava commented May 23, 2020

I have setup my locators like so

GetIt locator = GetIt.I;

Future<void> setupLocators() async {
  SharedPreferences prefs = await SharedPreferences.getInstance();
  final box = await Hive.openBox('user');

  locator.registerSingleton<UserService>(UserService(box),
      instanceName: 'User Service');
  locator.registerSingleton<SharedPreferences>(prefs,
      instanceName: 'Shared Preferences');
  locator.registerSingleton<SharedPrefsUtils>(SharedPrefsUtils(),
      instanceName: 'Shared Preferences Utils');

  locator.registerSingleton<AuthRepo>(AuthRepo(), instanceName: 'Auth Repo');
  locator.registerSingleton<AirtimeRepo>(AirtimeRepo(),
      instanceName: 'Airtime Repo');

  locator.registerSingleton<InternetBundleRepo>(InternetBundleRepo(),
      instanceName: 'Internet Bundle Repo');
  locator.registerSingleton<VoucherRepo>(VoucherRepo(),
      instanceName: 'Voucher Repo');
}

And this is my main method

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Hive.initFlutter();
  await setupLocators();
  BlocSupervisor.delegate = BlocDelegate();

  runApp(IBorrowApp());
}

and this is the IBorrowApp widget

class IBorrowApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider<AuthBloc>(
          create: (context) => AuthBloc(locator.get<AuthRepo>()),
        ),
        BlocProvider<AirtimeBloc>(
          create: (context) => AirtimeBloc(locator.get<AirtimeRepo>()),
        ),
        BlocProvider<InternetBundleBloc>(
          create: (context) =>
              InternetBundleBloc(locator.get<InternetBundleRepo>()),
        ),
        BlocProvider<VouchersBloc>(
          create: (context) => VouchersBloc(locator.get<VoucherRepo>()),
        ),
      ],
      child: MaterialApp(
        title: 'iBorrow',
        theme: Styles.lightTheme,
        onGenerateRoute: Router.onGenerateRoute(),
      ),
    );
  }
}

But when the app is run, I get an error

E/flutter (14500):  Did you forget to pass an instance name? 
E/flutter (14500): (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you forget to register it?)
E/flutter (14500): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:42:39)
E/flutter (14500): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:38:5)
E/flutter (14500): #2      _GetItImplementation._findFactoryByNameOrType (package:get_it/get_it_impl.dart:251:14)
E/flutter (14500): #3      _GetItImplementation.get (package:get_it/get_it_impl.dart:263:27)
E/flutter (14500): #4      new SharedPrefsUtils (package:iborrow/utils/prefs_utils.dart:10:22)
E/flutter (14500): #5      setupLocators (package:iborrow/utils/locator.dart:18:47)
E/flutter (14500): <asynchronous suspension>
E/flutter (14500): #6      main (package:iborrow/main.dart:16:3)
E/flutter (14500): <asynchronous suspension>
E/flutter (14500): #7      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:241:25)
E/flutter (14500): #8      _rootRun (dart:async/zone.dart:1184:13)
E/flutter (14500): #9      _CustomZone.run (dart:async/zone.dart:1077:19)
E/flutter (14500): #10     _runZoned (dart:async/zone.dart:1619:10)
E/flutter (14500): #11     runZonedGuarded (dart:async/zone.dart:1608:12)
E/flutter (14500): #12     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:233:5)
E/flutter (14500): #13     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (14500): #14     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (14500):
I/flutter (14500): 'package:get_it/get_it_impl.dart': Failed assertion: line 251 pos 14: 'instanceFactory != null': No type UserService is registered inside GetIt.
I/flutter (14500):  Did you forget to pass an instance name? 
I/flutter (14500): (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you forget to register it?)
I/flutter (14500): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter (14500): The following assertion was thrown building ActivationScreen(dependencies: [_InheritedTheme,
I/flutter (14500): _LocalizationsScope-[GlobalKey#93dc5]], state: _ActivationScreenState#ef53e):
I/flutter (14500): No type AuthRepo is registered inside GetIt.
I/flutter (14500):  Did you forget to pass an instance name?
I/flutter (14500): (Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;did you
I/flutter (14500): forget to register it?)
I/flutter (14500): 'package:get_it/get_it_impl.dart':
I/flutter (14500): Failed assertion: line 251 pos 14: 'instanceFactory != null'
I/flutter (14500):
I/flutter (14500): The relevant error-causing widget was:
I/flutter (14500):   ActivationScreen file:///C:/Users/Deana/Flutter/side_hustle/iborrow/lib/utils/router.dart:20:20
I/flutter (14500):
I/flutter (14500): When the exception was thrown, this was the stack:
I/flutter (14500): #2      _GetItImplementation._findFactoryByNameOrType (package:get_it/get_it_impl.dart:251:14)
I/flutter (14500): #3      _GetItImplementation.get (package:get_it/get_it_impl.dart:263:27)
I/flutter (14500): #4      _IBorrowAppState.build.<anonymous closure> (package:iborrow/main.dart:34:49)
I/flutter (14500): #5      _CreateInheritedProviderState.value (package:provider/src/inherited_provider.dart:576:35)
I/flutter (14500): #6      _InheritedProviderScopeElement.value (package:provider/src/inherited_provider.dart:439:33)
I/flutter (14500): #7      Provider.of (package:provider/src/provider.dart:216:29)
I/flutter (14500): #8      BlocProvider.of (package:flutter_bloc/src/bloc_provider.dart:105:23)
I/flutter (14500): #9      _BlocListenerBaseState.initState (package:flutter_bloc/src/bloc_listener.dart:160:41)
I/flutter (14500): #10     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4640:58)
I/flutter (14500): #11     ComponentElement.mount (package:flutter/src/widgets/framework.dart:4476:5)
I/flutter (14500): #12     SingleChildWidgetElementMixin.mount (package:nested/nested.dart:223:11)
I/flutter (14500): ...     Normal element mounting (123 frames)
I/flutter (14500): #135    Element.inflateWidget (package:flutter/src/widgets/framework.dart:3446:14)
I/flutter (14500): #136    Element.updateChild (package:flutter/src/widgets/framework.dart:3214:18)
I/flutter (14500): #137    RenderObjectElement.updateChildren (package:flutter/src/widgets/framework.dart:5580:32)
I/flutter (14500): #138    MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5957:17)
I/flutter (14500): #139    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #140    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
I/flutter (14500): #141    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter (14500): #142    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (14500): #143    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
I/flutter (14500): #144    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #145    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
I/flutter (14500): #146    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (14500): #147    ProxyElement.update (package:flutter/src/widgets/framework.dart:4862:5)
I/flutter (14500): #148    _InheritedNotifierElement.update (package:flutter/src/widgets/inherited_notifier.dart:181:11)
I/flutter (14500): #149    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #150    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
I/flutter (14500): #151    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #152    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
I/flutter (14500): #153    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter (14500): #154    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (14500): #155    StatefulElement.update (package:flutter/src/widgets/framework.dart:4707:5)
I/flutter (14500): #156    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #157    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
I/flutter (14500): #158    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #159    SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:5837:14)
I/flutter (14500): #160    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #161    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
I/flutter (14500): #162    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (14500): #163    StatelessElement.update (package:flutter/src/widgets/framework.dart:4583:5)
I/flutter (14500): #164    Element.updateChild (package:flutter/src/widgets/framework.dart:3201:15)
I/flutter (14500): #165    ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4527:16)
I/flutter (14500): #166    StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4675:11)
I/flutter (14500): #167    Element.rebuild (package:flutter/src/widgets/framework.dart:4218:5)
I/flutter (14500): #168    BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2627:33)
I/flutter (14500): #169    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:883:20)
I/flutter (14500): #170    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:284:5)
I/flutter (14500): #171    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1113:15)
I/flutter (14500): #172    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1052:9)
I/flutter (14500): #173    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:968:5)
I/flutter (14500): #177    _invoke (dart:ui/hooks.dart:261:10)
I/flutter (14500): #178    _drawFrame (dart:ui/hooks.dart:219:3)
I/flutter (14500): (elided 5 frames from class _AssertionError and dart:async)
I/flutter (14500):
I/flutter (14500): ════════════════════════════════════════════════════════════════════════════════════════════════════

Flutter doctor summary:

[√] Flutter (Channel stable, v1.17.1, on Microsoft Windows [Version 10.0.18363.836], locale en-US)
 
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
[√] Android Studio (version 3.6)
[√] VS Code, 64-bit edition (version 1.45.1)
[√] Connected device (1 available)

• No issues found!

UPDATE: Added the code for the IBorrowApp.
Side note: I do not use any relative imports in the app, all my imports are at the package level.

@escamoteur
Copy link
Collaborator

Why are you use instance names? If you use them when registering you have to pass the name when you access the instance with get.

@danagbemava
Copy link
Author

danagbemava commented May 25, 2020

I added the instance names because I was getting the error posted in the initial comment.

Initially, they were working fine without the instance name, but it just stopped registering until I added the instance names.

After adding the instance names and calling get with them, the application is working fine.

I find the behavior strange because I have other projects in which I use get_it and don't use the instance names and they all work fine.

I even tried copying a working configuration from one of my projects and changing the instances to match the current project classes, but it still failed.

@escamoteur
Copy link
Collaborator

This sounds really weird. Did you always use the gerneric types when using without names?

@danagbemava
Copy link
Author

Yes.
So I decided to do some debugging and I took out all the instance names across the entire project.
It is working for now, so this issue can be closed.
However, I will try to investigate and see what actually caused it to fail all of a sudden and share my findings.

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