Closed Kiwi-KiwiCorp closed 2 years ago
Hi @Kiwi-KiwiCorp
Honestly, i wasn't aware that such mixen exists in Flutter, so thanks a lot for the tip ✌️, i will handle this within 1:2 days
That being said, the disposeVM
parameter of the MVVM
widget was meant to handle such a case. When it's set to false
, this enables us to pass an existing instance of the VM so that when you cycle through the tabs it doesn't get disposed
In the upcoming version you should be able to do the same through the disposeVM
but without creating an instance manually and passing it to the MVVM
widget
Hi @Kiwi-KiwiCorp
Honestly, i wasn't aware that such mixen exists in Flutter, so thanks a lot for the tip ✌️, i will handle this within 1:2 days
That being said, the
disposeVM
parameter of theMVVM
widget was meant to handle such a case. When it's set tofalse
, this enables us to pass an existing instance of the VM so that when you cycle through the tabs it doesn't get disposedIn the upcoming version you should be able to do the same through the
disposeVM
but without creating an instance manually and passing it to theMVVM
widget
If that is the case then I'm pretty sure there is a bug with the disposeVM param as I tried this and the only difference it made was the fact that there was no error when switching between the tabs. If disposeVM is set to true and you click away from a tab and then click back on the tab it errors out. Also, it appears initOnce makes no difference as well due to the fact that even with disposeVM set to false the VM still gets disposed when clicking on another tab.
I'm not sure about what the other use cases are, however, what I would love to see is 3 simple parameters:
keep alive would simply switch the functionality from disposing the widget and VM when the widget is not in view to keeping the widget alive and simply calling the onPause and onResume methods when the widget is visible and not visible. The 'AutomaticKeepClientAlive' mixin has a param that you can set to keep alive true/false so you could simply hook the param up to this (i think).
Hi @Kiwi-KiwiCorp
I'm going to answer all your points in this comment, so let's get started =D:
AutomaticKeepAliveClientMixin
works, i believe this is out of the scope PMVVM package (state management) since it changes the behavior of the build, dispose, and intState methods. The view should be responsible for this, so i would suggest making a wrapper class and calling it KeepAliveWrapper
and making the MVVM
its child.class KeepAliveWrapper extends StatefulWidget {
const KeepAliveWrapper({Key? key, required this.child}) : super(key: key);
final Widget child;
@override
_KeepAliveWrapperState createState() => _KeepAliveWrapperState();
}
class _KeepAliveWrapperState extends State<KeepAliveWrapper> with AutomaticKeepAliveClientMixin {
@override
Widget build(BuildContext context) {
super.build(context);
return widget.child;
}
@override
bool get wantKeepAlive => true;
}
onResume
, onPause
, onInactive
, and onDetach
are application life cycle methods... There are many packages that provide widgets that can help you detect whether the widget is visible or not, again, this issue has nothing to do with state management 😉. However, in v4.0.0 the view model has two callbacks onMount
and onUnmount
, they are triggered when the view of the MVVM widget is mounted/unmountedonMount
and onUnmount
methods will change if they are wrapped with AutomaticKeepAliveClientMixin
, for example the onMount
will be triggered only once when the tab is opened for the first time and onUnmount
will never be invoked even if the tab was changed, this is how the AutomaticKeepAliveClientMixin
works
initOnce
parameter in v4.0.0, the init
method will only be called once after MVVM widget initState
is called, it seems that this parameter is confusing people of PMVVM works.onDependenciesChange
that gets triggered when the didChangeDependencies
of the MVVM widget is called.disposeVM
and the error that is being raised, can u provide a code sample as i can't reproduce this issue from my side.Hi @Kiwi-KiwiCorp
I'm assuming the issue is fixed so i will close it for now, and feel free to reopen it if u have any other questions =D
as far as I can figure out, there is no option like the 'AutomaticKeepClientAlive' mixin to keep the widget from being disposed of when clicking away. The desired behaviour I'm looking for is a page with tabs. each tab has its own MVVM page with a view and a ViewModel. Cycling through each tab should pause the last opened tab rather than disposing of it and maintain the state resuming rather than rebuilding the viewModel every time you click on the tab after it has been initialised. As far as I can tell, this is not possible with PMVVM currently and is stopping me from using this very appealing package.