Skip to content

registerLazySingleton unexpected behavior #39

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
computervirus91 opened this issue Jan 13, 2020 · 9 comments
Closed

registerLazySingleton unexpected behavior #39

computervirus91 opened this issue Jan 13, 2020 · 9 comments

Comments

@computervirus91
Copy link

This does NOT WORK,
GetIt.I.registerLazySingleton(() => SingletonInjector.configuration(RepoType.PROD));

ERROR,
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following ArgumentError was thrown building Builder(dirty):
Invalid argument (Object of type Injector is not registered inside GetIt.
Did you forget to pass an instance name?
(Did you accidentally do GetIt sl=GetIt.instance(); instead of GetIt sl=GetIt.instance;)): Instance
of '_Type'

But this WORKS,
Injector inj = SingletonInjector.configuration(RepoType.PROD);
GetIt.I.registerLazySingleton(() => inj);

@escamoteur
Copy link
Collaborator

Which version are you using?

@computervirus91
Copy link
Author

Which version are you using?

get_it: 3.1.0

@escamoteur
Copy link
Collaborator

This is definitively strange. I have a closer look

@escamoteur
Copy link
Collaborator

It works for me see my Test Code:

  test('register LazySingleton with lambda and factory function', () {
    GetIt.I.registerLazySingleton(() => SingletonInjector.configuration());

    final instance = GetIt.I<Injector>();

    expect(instance, TypeMatcher<Injector>());
  });
}

class SingletonInjector {
  static Injector configuration() {
    return Injector();
  }
}

class Injector {}

I would have to see the code for your injector and SinglettonInjector.

Overall I want to add, you should always provide a type or a name when registering something, you didn't either. it only worked because of type inference.
My guess would be that configurationdoes not have a return type declared.

@escamoteur
Copy link
Collaborator

Please also test the latest beta #46

@computervirus91
Copy link
Author

computervirus91 commented Feb 14, 2020

//########################################################################
// injector_interface.dart
----------------------------

enum RepoType {
  MOCK,
  PROD,
}

abstract class Injector{
  //getter and setter
  LocationTracker get locationTracker;
  Repository get repo;

  //method
  void configureInjector(RepoType repoType);

  void dispose();
}
//#######################################################################
//singleton_injector.dart
---------------------------


class SingletonInjector implements Injector {
  //class property
  static final SingletonInjector _singleton = SingletonInjector._internal();
  static RepoType _repoType;

  //getter and setter

  //class constructor
  factory SingletonInjector.configuration(RepoType repoType) {
    _configure(repoType);
    return _singleton;
  }

  SingletonInjector._internal();

  //class methods
  static void _initRepo() {
    //repository
    switch (_repoType) {
      case RepoType.MOCK:
        GetIt.I.registerLazySingleton<Repository>(() => RepoPROD());
        break;
      default:
        GetIt.I.registerLazySingleton<Repository>(() => RepoPROD());
    }

    //location tracker
  }

  //configure injector methods
  @override
  void configureInjector(RepoType repoType) {
    _configure(repoType);
  }

  //config wrapper
  static void _configure(RepoType repoType) {
    print("_configure from injector");
    GetIt.I.reset();
    _repoType = repoType;
    _initRepo();
    kIsWeb ? _configureForWeb() : _configureForMobile();
  }

  @override
  void dispose() {
    repo.cloudDB.dispose();
    GetIt.I.reset();
  }
}

@computervirus91
Copy link
Author

It works for me see my Test Code:

  test('register LazySingleton with lambda and factory function', () {
    GetIt.I.registerLazySingleton(() => SingletonInjector.configuration());

    final instance = GetIt.I<Injector>();

    expect(instance, TypeMatcher<Injector>());
  });
}

class SingletonInjector {
  static Injector configuration() {
    return Injector();
  }
}

class Injector {}

I would have to see the code for your injector and SinglettonInjector.

Overall I want to add, you should always provide a type or a name when registering something, you didn't either. it only worked because of type inference.
My guess would be that configurationdoes not have a return type declared.

this is the code for injector and its singleton implementation

@escamoteur
Copy link
Collaborator

Yeah, I guess Dart has a problem infferring the type for factory constructors please do

`` Dart
GetIt.I.registerLazySingleton(() => SingletonInjector.configuration(RepoType.PROD));


which is the correct way if you don't use a named instance

@computervirus91
Copy link
Author

Yeah, I guess Dart has a problem infferring the type for factory constructors please do

`` Dart
GetIt.I.registerLazySingleton(() => SingletonInjector.configuration(RepoType.PROD));


which is the correct way if you don't use a named instance

thank you for your reply

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