Skip to content

ResetLazySingleton not working #108

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
Lzyct opened this issue Sep 18, 2020 · 4 comments
Closed

ResetLazySingleton not working #108

Lzyct opened this issue Sep 18, 2020 · 4 comments

Comments

@Lzyct
Copy link

Lzyct commented Sep 18, 2020

I'm try to register class User as LazySingletone like this

 User _user = User.fromJson(json.decode(sl<PrefManager>().getUser()));
  try {
    sl.registerLazySingleton<User>(() => _user, instanceName: "user");
  } catch (e) {
    print("error 1: $e");
    try {
      sl.resetLazySingleton<User>(
          instanceName: "user", instance: _user, disposingFunction: (user) {});
    } catch (e) {
      print("error 2: $e");
      sl.registerLazySingleton<User>(() => _user, instanceName: "user");
    }
  }

but when i'm try to reset previous data User with resetLazySingletone i got this error :

flutter: error 1: Invalid argument(s): Object/factory with with name user and  type User is already registered inside GetIt.
flutter: error 2: Bad state: There is no object type User registered in GetIt
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Invalid argument(s): Object/factory with with name user and  type User 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:675:5)
#2      _GetItImplementation.registerLazySingleton (package:get_it/get_it_impl.dart:455:5)
#3      registerUser (package:brupedia/di/services_locator.dart:50:10)
#4      _MainPageState._init (package:brupedia/pages/main/main_page.dart:51:11)
#5      _MainPageState.initState (package:brupedia/pages/main/main_page.dart:41:5)
#6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
#8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
#10     ComponentElement.performRebuild (package:flutter/<…>
@escamoteur
Copy link
Collaborator

Which version of get_it are you using?

some remarks:

  • you don't have to provide an instanceName if you don't plan to use multiple objects of the same type in GetIt
  • When you already have the object that you want to register it doesn't make sense to use a lazy Singleton.
  • your disposing functions don't do anything, so you can omit them
  • resetLazySingleton does not remove the registration but only clear the created instance, so the next time you will call get the factory function is called again. You would have to use unresgister instead but I recommend using the new scope feature in get_it V5.0.0

@Lzyct
Copy link
Author

Lzyct commented Sep 19, 2020

i'm using latest get_it
Btw could you suggest to me best practice to reset singleton, i have singleton user to provide user information but when logout and login with different account it's still load old data, so i'm trying to reset it.
Thanks

@Lzyct
Copy link
Author

Lzyct commented Sep 19, 2020

Because it's make me confusing

 User _user = User.fromJson(json.decode(sl<PrefManager>().getUser()));
  try {
    sl.registerLazySingleton<User>(() => _user);
  } catch (e) {
    print("error $e");
    sl.resetLazySingleton<User>(
      instance: _user,
    );
  }

But give me error like this

flutter: error Invalid argument(s): Object/factory with  type User is already registered inside GetIt.
[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: Bad state: There is no object type User registered in GetIt
#0      throwIf (package:get_it/get_it_impl.dart:7:18)
#1      _GetItImplementation._findFactoryByInstance (package:get_it/get_it_impl.dart:880:5)
#2      _GetItImplementation.resetLazySingleton (package:get_it/get_it_impl.dart:851:25)
#3      registerUser (package:brupedia/di/services_locator.dart:45:8)
#4      _MainPageState._init (package:brupedia/pages/main/main_page.dart:51:11)
#5      _MainPageState.initState (package:brupedia/pages/main/main_page.dart:41:5)
#6      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4765:58)
#7      ComponentElement.mount (package:flutter/src/widgets/framework.dart:4601:5)
#8      Element.inflateWidget (package:flutter/src/widgets/framework.dart:3569:14)
#9      Element.updateChild (package:flutter/src/widgets/framework.dart:3327:18)
#10     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4652:16)
#1<…>

error in try it's tell me is already register but when i'm trying to reset it's give me error no object register with type User ?

@escamoteur
Copy link
Collaborator

Just think a moment what a LazySingleton is compared to a Singleton? It's instance is only created when you first access it.
In the code above you register it and you try to unregister it by using the instance parameter. you didn't access it once after registration. So how could getIt find this instance?

For what you want I would recommend you use the new scope features. before you create a new user object you call pushScope and when your user logs out you pop that scope and you are fine. And don't use a Lazysingleon but a normal one.

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