Stacked-Org/stacked

[bug]: assertion was thrown while dispatching notifications for TextEditingController

Closed this issue · 2 comments

Describe the bug

This happens in web template with form view.
When you change the size of the browser window, the ScreenTypeLayout.builder kicks in and changes the view from Mobile to Tablet to Desktop.

During this change the assertion occurs (details here)

══╡ EXCEPTION CAUGHT BY FOUNDATION LIBRARY ╞════════════════════════════════════════════════════════
The following assertion was thrown while dispatching notifications for TextEditingController:
Looking up a deactivated widget's ancestor is unsafe.
At this point the state of the widget's element tree is no longer stable.
To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by
calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.

To reproduce

  1. Create a login form with web template
  2. add annotations to login_view.dart
@FormView(
  fields: [
    FormTextField(
      name: 'email',
      validator: FormValidators.emailValidator,
    ),
    FormTextField(
      name: 'password',
      validator: FormValidators.passwordValidator,
      customTextEditingController:
          CustomEditingController.getCustomEditingController,
    ),

  1. The login_view.dart will have following
 @override
  Widget builder(
    BuildContext context,
    LoginViewModel viewModel,
    Widget? child,
  ) {
    return ScreenTypeLayout.builder(
      mobile: (_) => const LoginViewMobile(),
      tablet: (_) => const LoginViewTablet(),
      desktop: (_) => const LoginViewDesktop(),
    );
  }

  1. Ensure all the three mobile/tablet/desktop views have the form elements (as documented)
  2. Launch the app on chrome
  3. change the size of the chrome window to switch from each size.
  4. As soon as one size changes (either from mobile to tablet or tablet to desktop or vice versa) - exception occurs
  5. Post that form fields fail in everything

Expected behavior

When the "ScreenTypeLayout.builder" triggers the change

  1. Handle gracefully
  2. Ensure the form works as expected.

Screenshots

No response

Additional Context

No response

I think I can see one of the reason why this is happening.
The following statement is not getting called
onDispose: (model) => disposeForm(),

The problem occurs also when you navigate to different view (HomeView). The form is not getting disposed.

If the homeView is still in the stack that won't be called. It will only be called when the home view is removed from the stack. The dispose follows Flutter's built in dispose cycle, we don't control it, we just call our function when they call theirs.