Skip to content

Object/Factory not registered but it is #142

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
emri99 opened this issue Dec 18, 2020 · 7 comments
Closed

Object/Factory not registered but it is #142

emri99 opened this issue Dec 18, 2020 · 7 comments

Comments

@emri99
Copy link

emri99 commented Dec 18, 2020

Hello,

I'm experimenting a weird behavior resulting in a fatal error.

Installed versions:

  • Get_it version: 5.0.3
  • Injectable version: 1.0.5

Get_It report a class unregistered but it is and my teammates does not experiment this issue with exactly the same codebase. (I already tried few things like cloning our repo again from scratch in another folder, resetting the Android Studio local project.)

The error message is:

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: 'package:get_it/get_it_impl.dart': Failed assertion: line 298 pos 9: 'instanceFactory != null': Object/factory with  type IAccountService is not registered inside GetIt. 
(Did you accidentally do  GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;
Did you forget to register it?)
#0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
#1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
#2      _GetItImplementation._findFactoryByNameAndType (package:get_it/get_it_impl.dart:298:9)
#3      _GetItImplementation.get (package:get_it/get_it_impl.dart:313:29)
#4      _GetItImplementation.call (package:get_it/get_it_impl.dart:339:12)
#5      new AccountViewModel (package:app_mobile/ui/views/account/AccountViewModel.dart:60:42)
#6      $initGetIt (package:app_mobile/app/locator.config.dart:70:35)
<asynchronous suspension>
#7      configureInjection (package:app_mobile/app/locator.dart:9:44<…>

In debug mode, this class is present in the internal hash map factoryByTypes from get_it_impl.dart:282

Here is a screen capture of Android Studio debug panel showing that factoryByTypes includes the IAccountService in position 4 but instanceFactory = null as factoryByTypes[T] = null.
Capture d’écran 2020-12-18 à 20 43 32

Here are the few files sections related to this error:

// AccountService.dart

abstract class IAccountService extends BaseService {
    // ...
}

@Injectable(as: IAccountService)
class AccountService extends IAccountService {
    // ...
}
// AccountViewModel.dart

final locator = GetIt.I;

abstract class IAccountViewModel {
  // ...
}

@Singleton(as: IAccountViewModel)
class AccountViewModel extends IAccountViewModel {
  final IAccountService service = locator<IAccountService>();
  // ....
}
// locator.dart
// ...
final locator = GetIt.I;

@injectableInit
Future<void> configureInjection() async => await $initGetIt(locator);
// locator.config.dart (generated file using injectable)
// ...
Future<GetIt> $initGetIt(
  GetIt get, {
  String environment,
  EnvironmentFilter environmentFilter,
}) async {
  final gh = GetItHelper(get, environment, environmentFilter);
  final servicesModule = _$ServicesModule();
  // ...
  gh.factory<IAccountService>(() => AccountService());
  // ...
  // Eager singletons must be registered in the right order
  gh.lazySingleton<IAccountViewModel>(() => AccountViewModel());
  // ...
  return get;
}
// main.dart
void main() async {
  // ...
  await configureInjection();
  runApp(...)
}

I'm trying to fix this for few days in vain.
I don't understand what's going wrong.
Any help would be greatly appreciated ;)

@escamoteur
Copy link
Collaborator

Have you done a flutter clean ?
this sounds indeed weird. If you can share the project by adding me to your repo I can try to reproduce. What I don't understand is why your collegue has no problems with this, which sounds more like a problem of your setup than one of get_it.

@emri99
Copy link
Author

emri99 commented Dec 25, 2020

Thanks for your response.
flutter clean doesn't help.
It appears that my teammate experiment the same issue at the end. I'm still unsure why this was not the case at the time writing this post.
Unfortunately, currently, I can't share the code as is. I'll try to isolate the problem, so I can create a specific repo to demonstrate our problem.
A partly solution found has been to move a generated line (doesn't have more details, as I'm on vacation and this is my teammates who perform this temporary fix).
I'll be back to expand explanation as soon as it's possible ;)

@emri99
Copy link
Author

emri99 commented Jan 6, 2021

We found the issue reason.

Short version:
It was due to a refactoring of filename: we change some filename case.

Long version:
We are working on MacOs and my teammate has probably setup an insensitive case filesystem and I don't.
So when he perform refactoring, the new filenames wasn't reflected in our git repo, and I still get the previous version of filenames which produced this weird bug.

It's a pity that Android Studio doesn't flag filename missing on import when there's a case problem. That would have save us a lot of wasted time ;)
Thanks for your help and this library 👍

@emri99 emri99 closed this as completed Jan 6, 2021
@HussainON
Copy link

I have the same issue. I have been looking for an a solution for two days now. https://github.com/HussainON/authApp.git Here is the code if anyone can look it up and help me please.

@HussainON
Copy link

Hello @escamoteur I added you to a repo authApp. Please help! I would appreciate it

@imamabdulazis
Copy link

I have the same issue. I have been looking for an a solution for two days now. https://github.com/HussainON/authApp.git Here is the code if anyone can look it up and help me please.

this repo doesn't exist,
have you make sure call get it before runApp() ?

@ziqq
Copy link

ziqq commented Mar 18, 2022

My issue was the wrong case of the model import. '../auth_viewModel.dart' must be '../auth_viewmodel.dart'

image

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

5 participants