skip to Main Content

When I try to launch my flutter app on an iOS emulator I get a white blank screen and this:

 Launching lib/main.dart on iPhone 12 Pro Max in debug mode...
Running Xcode build...                                                  
 └─Compiling, linking and signing...                        13.7s
Xcode build done.                                           38.0s
[VERBOSE-2:ui_dart_state.cc(186)] Unhandled Exception: Invalid argument(s): Object/factory with  type Api is already registered inside GetIt. 
#0      throwIf (package:get_it/get_it_impl.dart:7:18)
#1      _GetItImplementation._register (package:get_it/get_it_impl.dart:729:5)
#2      _GetItImplementation.registerLazySingleton (package:get_it/get_it_impl.dart:502:5)
#3      setupLocator (package:r/locator.dart:14:18)
#4      main (package:r/main.dart:12:3)
#5      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:140:25)
#6      _rootRun (dart:async/zone.dart:1354:13)
#7      _CustomZone.run (dart:async/zone.dart:1258:19)
#8      _runZoned (dart:async/zone.dart:1788:10)
#9      runZonedGuarded (dart:async/zone.dart:1776:12)
#10     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:133:5)
#11     _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:283:19)
#12     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.<…>
Syncing files to device iPhone 12 Pro Max...                       137ms

2

Answers


  1. It’s late for your question but I try to explain the problem to those who still face such an error, I hope it helps.

    What basically is happening is that a dependency, registered in GetIt instance is being re-registered at some point.
    There are three possible solutions

    1. Use Safe Registration i.e. check whether the type you want to register is already registered, only if it is not registered yet, call the register method.
        if (!GetIt.instance.isRegistered<T>()) {
        GetIt.instance.registerLazySingleton<T>(() => const T());}
    
    1. Find out where the dependency is being registered for the second time and prevent the re-registration by removing the line
    2. Try to unregister your dependency after you’re done with the instance and re-register it again whenever you need it. (probably not the best solution).
      if (GetIt.instance.isRegistered<T>()) {
        GetIt.instance.unregister<T>(); }
    

    Inspired by This.

    Login or Signup to reply.
  2. This happened to me when I was re-registering my services accidentally in setUp in a test. Fixed by using setUpAll

      // Register them here
      setUpAll(() {
        exampleService = ExampleService(MockExampleRepository());
        final getIt = GetIt.instance;
        getIt.registerSingleton<ExampleService>(exampleService);
      });
    
      // Not here.
      setUp(() {});
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search