VB10 / flutter-architecture-template

Flutter Architecture Complete App
https://vb10.dev/#/
Apache License 2.0
650 stars 130 forks source link

Calling onDispose method #47

Closed behzodfaiziev closed 10 months ago

behzodfaiziev commented 1 year ago

I found out that dispose method of TextEditingController in LoginViewModel is not called when dispose should be called. In BaseView class dispose is mentioned, however it was not implemented in LoginView. I tried to solve it, but I couldn't dispose, because dispose's type is VoidCallback, so it is not possible to get viewModel (LoginViewModel) in dispose method. So I changed:

  final VoidCallback? onDispose; 
  .
  .
  @override  void dispose() {
    super.dispose();
    if (widget.onDispose != null) widget.onDispose?.call();
  }

to

  final Function(T model)? onDispose;
  .
  .
  .
 @override
  void dispose() {
     if (widget.onDispose != null) widget.onDispose!(model);
     super.dispose();

  }

and called dispose in this way in LoginView

   return BaseView<LoginViewModel>(
      viewModel: LoginViewModel(),
      onModelReady: (model) {
        model.setContext(context);
        model.init();
      },
      onDispose: (model) {
        model.dispose();
      },

I hope it is helpful. I might be completely wrong, so take it easy :)