get_it icon indicating copy to clipboard operation
get_it copied to clipboard

got a problem with getIt.isReady?

Open freewebwithme opened this issue 2 years ago • 2 comments

First I try to register PhoenixSocket type and to make sure that GraphQLClient is registered I check

  print(getIt.isRegistered<GraphQLClient>());

  // Register Phoenix Socket
  getIt.isReady<GraphQLClient>().then((value) =>
      getIt.registerSingletonAsync<PhoenixSocket>(
          ()  =>  Client().initializeSocket()));

And here isinitializeSocket()

class Client {
  Box? box;
  String? token;

  Client() {
    box = getIt<Box>();
    token = box!.get("userToken");
  }

  Future<PhoenixSocket> initializeSocket() async {
    print("Initializing Socket...");
    return PhoenixSocket(
      wsEndpoint,
      socketOptions: PhoenixSocketOptions(
        //params: {"token": token},
        dynamicParams: (() => getAToken()),
      ),
    );
  }
}

And register another type that depends on PhoenixSocket type.


  getIt.isReady<PhoenixSocket>().then((value) => getIt
      .registerSingleton<ICartChannel>(CartChannel(getIt<PhoenixSocket>())));

But I got an error

E/flutter (10182): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 372 pos 7: 'instanceFactory != null': Object/factory with  type PhoenixSocket is not registered inside GetIt. 
E/flutter (10182): (Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
E/flutter (10182): Did you forget to register it?)
E/flutter (10182): #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
E/flutter (10182): #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
E/flutter (10182): #2      _GetItImplementation._findFactoryByNameAndType (package:get_it/get_it_impl.dart:372:7)
E/flutter (10182): #3      _GetItImplementation.isReady (package:get_it/get_it_impl.dart:1286:24)
E/flutter (10182): #4      afterAuthenticationGetIt (package:myApp/getit_setup.dart:108:9)
E/flutter (10182): #5      AuthenticationApi.status (package:myApp/api/apis/authentication_api.dart:74:17)
E/flutter (10182): <asynchronous suspension>
E/flutter (10182): #6      new AuthenticationBloc.<anonymous closure> (package:myApp/authentication/bloc/authentication_bloc.dart:21:17)
E/flutter (10182): <asynchronous suspension>

I tried to find a bug but it took me a day and still can't figure out! Please give me an advice!

freewebwithme avatar Mar 31 '22 21:03 freewebwithme

You have to register PhoenixSocket an inside GetIt . I want to see how you get get_it instance because the stacktrace to clair .

AdamMusa avatar Apr 03 '22 12:04 AdamMusa

you can use the dependsOn parameter to ensure that your first object is ready before the second is registered

escamoteur avatar Apr 15 '22 10:04 escamoteur