Skip to content

No type Api is registered inside GetIt. #199

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
Octonove opened this issue Jun 8, 2021 · 1 comment
Closed

No type Api is registered inside GetIt. #199

Octonove opened this issue Jun 8, 2021 · 1 comment

Comments

@Octonove
Copy link

Octonove commented Jun 8, 2021

Hi!

One of the people that I work with had an issue and cant keep working on a project, and now I have to finish it.

This project uses Flutter Web and Get_it (besides other packages) and I'm trying to understand how it works

But when I try to run it i have this error

"No type Api is registered inside GetIt.\n Did you forget to pass an instance name? \n(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;\ndid you forget to register it?)"

From what I can understand, the flow goes...

Main.dart

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await initConfig();
  runApp(App());
}

injectable.dart


final GetIt getIt = GetIt.instance;
ThemeColor themeColor;
SoundLibrary sounds = SoundLibrary();

@injectableInit
Future<void> initConfig() async {
  $initGetIt(getIt, environment: Env.env);
  await sounds.init();
  await getIt.getAsync<ConfigLoader>();
  themeColor = ThemeColor();
}

ConfigLoader.dart

@lazySingleton
class ConfigLoader {
  ColorModel colors;

  @factoryMethod
  static Future<ConfigLoader> load() async {
     final Api api = getIt<Api>();  ----> HERE IS THE PROBLEM
     final ConfigLoader loader = ConfigLoader();
     final Response<dynamic> response = await api.client.get('/loadConfig.json');
     if(response.data != null){
       loader.colors = ConfigyModel.fromMap(response.data).colors;
     }
     return loader;
  }
}

I think the problem is when I make GetIt(), the system searches for Api but it "has not been " registered.

But when I took a look at this generated file

GetIt $initGetIt(
  GetIt get, {
  String environment,
  EnvironmentFilter environmentFilter,
}) {
  final GetItHelper gh = GetItHelper(get, environment, environmentFilter);
  gh.factory<Api>(() => ApiProd(), registerFor: {_prod});
  gh.factory<Api>(() => ApiDev(), registerFor: {_dev});
  gh.lazySingleton<AuthRepository>(() => AuthRepository(get<Api>()));
....

From what is see here, the Api class has been registered (Api is an abstract class)

Can someone help me?

@escamoteur
Copy link
Collaborator

Hi, this uses the injectable package on top of get_it. Please ask at their repository

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