You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
We are using get_it here to totally handle our dependencies instances, and we are using clean architecture as suggested by reso coder tutorial, so we have several layers (viewModel -> usecase -> repository -> data_source), let me show some snippets.
getIt.registerLazySingleton(
() => MyViewModel(getIt()),
);
getIt.registerLazySingleton(
() => Usecase(getIt()),
);
getIt.registerLazySingleton<AbstractRepository>(
() => ConcreteRepository(getIt()),
);
getIt.registerLazySingleton(
() => RemoteDataSource(getIt()),
);
//this is the last one
getIt.registerLazySingleton(
() => CustomHttpClient(),
);
And what we are doing is: on the screen, we call final viewModel = getIt<MyViewModel>(), and all the instances are retrieved by a cascading call, lets see:
Our ViewModel needs a UseCase on it's constructor,
UseCase needs a Repository
Repository needs a DataSource
DataSource needs an ApiClient.
And all of these are being resolved by get_it, so when we call: final viewModel = getIt<MyViewModel>().
get_it is creating instances of MyViewModel, Usecase, ConcreteRepository, RemoteDataSource and CustomHttpClient, right?
This is really fantastic, because we don't have to worry about objects instantiations and it works well.
My questions
1 - If I call resetLazySingleton<MyViewModel>() on dispose method of my screen, will get_it cascading reset all the other dependencies?
The real question is:
What is the correct way to dispose these objects when we don't need them anymore?
The text was updated successfully, but these errors were encountered:
I know that it's been a long time, but I had to make a longer break due to health issues.
The short answer to your question is 'no', reset is not going to cascade.
Two questions come to my mind. if your objects are only that short-lived, why not using registerFactory and let the garbage collector take care about it.
The other option would be using a GetIt scope, meaning you register the objects on creation in a new scope and pop that scope in the widgets dispose function. The get_it_mixin offers an automatic function for that.
thinking a bit more about your scenario, it might be a nice addition to GetIt to add a resetLazySingeltonsInScope. #320
We are using
get_it
here to totally handle our dependencies instances, and we are using clean architecture as suggested by reso coder tutorial, so we have several layers (viewModel -> usecase -> repository -> data_source), let me show some snippets.File structure:
we have get_it registration file, like this:
And what we are doing is: on the screen, we call
final viewModel = getIt<MyViewModel>()
, and all the instances are retrieved by a cascading call, lets see:ViewModel
needs aUseCase
on it's constructor,UseCase
needs aRepository
Repository
needs aDataSource
DataSource
needs anApiClient
.And all of these are being resolved by get_it, so when we call:
final viewModel = getIt<MyViewModel>()
.get_it is creating instances of
MyViewModel
,Usecase
,ConcreteRepository
,RemoteDataSource
andCustomHttpClient
, right?This is really fantastic, because we don't have to worry about objects instantiations and it works well.
My questions
1 - If I call
resetLazySingleton<MyViewModel>()
ondispose
method of my screen, will get_it cascading reset all the other dependencies?The real question is:
What is the correct way to dispose these objects when we don't need them anymore?
The text was updated successfully, but these errors were encountered: