-
-
Notifications
You must be signed in to change notification settings - Fork 153
ResetLazySingleton is not resetting. #170
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
Comments
There is bug in get_it_impl class
to :
registrationType will do the magic |
@saharvx9 That's not correct. That part of the code is absolutely correct. @petermusembi69 I just looked at the code. How do you know it didn't reset? |
@saharvx9 a bit more explanation. The function you want to change is only called when you pass an instance of an Object which @petermusembi69 doesn't do here. If you needed to make the change that you proposed to get your code working you are using |
@petermusembi69 just a tip, for situations like that I recommend to check if get_it scopes wouldn't be the better way to go. |
I check dozen of times instance is always null |
if instance is null @override
void resetLazySingleton<T extends Object>({
Object? instance,
String? instanceName,
void Function(T)? disposingFunction,
}) {
_ServiceFactory instanceFactory;
if (instance != null) {
instanceFactory = _findFactoryByInstance(instance);
} else {
instanceFactory = _findFactoryByNameAndType<T>(instanceName); |
couly you show me the line whare you call |
@saharvx9's approach doesn't help much. @escamoteur this is how I know it didn't reset. Here are the cubit logs, thrown when the app is running. I have 4 states(initial, loading, loaded, error). 1st try with wrong LogIn credentials.
2nd try with the correct credentials starts off with the error state. This should not be the case if the lazySignleton was reset. It should start with the initial state instead.
3rd try with the correct credentials
|
|
@petermusembi69 can it be that you have somewhere else a reference to this Cubit so that the garbage collector can't do its job? I'm not familiar with Cubit but AFAIK it used streams, so you will have to call some dispose method on the cubid or not? |
|
could you solve your problem? |
@escamoteur Not yet, I will have a look at it, in the course of the week and share any useful findings. |
Cubit is a high-level library and it extends dart streams in the background. Using BlocProvider will automatically dispose the cubit. |
@escamoteur I have the same problem here but in the version |
try this small test - Version: 7.0.0 class Value {
int number = 0;
}
GetIt.I.registerLazySingleton<Value>(() => Value());
final value = GetIt.I.get<Value>();
value.number = 10;
print(value.number); // 10 - OK
GetIt.I.resetLazySingleton<Value>(instance: value);
final value2 = GetIt.I.get<Value>();
print(value2.number); // 10 instead of 0 |
oh, sorry you now have to await the reset |
I recommend checking out the changelog |
OK, It's working 👍🏾 |
However, if I do not have a disposing function in my I think changing this in the functions In if (instanceFactory.instance! = null) {
if (disposingFunction! = null) {
await disposingFunction.call (instanceFactory.instance as T);
} else {
await instanceFactory.dispose ();
}
} to: if (instanceFactory.instance != null) {
if (disposingFunction != null) {
final dispose = disposingFunction.call(instanceFactory.instance as T);
if (dispose is Future) {
await dispose;
}
} else {
final dispose = instanceFactory.dispose();
if (dispose is Future) {
await dispose;
}
}
} |
Hmm, that's an interesting take honestly I hadn't thought about that possibility. Would you like to make a PR and add tests to secure that it works as it should? |
OK, I created a pull request -> #185 |
Just pushed V7.1.2 @petermusembi69 could you please check if this version still has the problem for you? |
@escamoteur Version V7.1.2 solved the problem. I was able to get the desired behaviour from the cubit as shown in the logs below. Thank you.
|
I await the reset the reset, but does not work |
Uh oh!
There was an error while loading. Please reload this page.
Hello there,
I am implementing a login feature using getIt and cubit. So once the user clicks on the login button the desired behaviour is to reset the lazySingleton Logincubit. This is necessary when the login credentials are wrong. The app should be able to restart the auth process afresh.
I am using
get_it: ^6.0.0
Here is where I add the resetLazySingleton in the UI in the BlocConsumer
and the Login button.
Here is where I register my LazySingleton
The text was updated successfully, but these errors were encountered: