Skip to content

Updating selected items for DropdownSearch<T>.multiSelection throws already disposed exception #548

Closed
@cybex-dev

Description

Describe the bug
Throws exception upon calling setState() when selected items have changed for DropdownSearch<T>.multiSelection.

A simple if(mounted){} resolved the (immediate) issue.

Code selection for context

class _Interests extends StatelessWidget {
  const _Interests({super.key});

  @override
  Widget build(BuildContext context) {
    final pageBloc = context.read<PageBloc>();
    return BlocBuilder<PageBloc, PageState>(
      buildWhen: (previous, current) => previous.interests != current.interests,
      builder: (context, state) {
        final interests = state.interests;

        return BlocBuilder<InterestPreferenceBloc, InterestPreferenceState>(
          builder: (context, state) {
            List<String> list = (state is InterestsLoaded ? state.interests.map((e) => e.value).toList() : <String>[]).addAllWith(interests);

            return DropdownSearch<String>.multiSelection(
              onChanged: (value) => context.read<PageBloc>().updateInterests(value),
              selectedItems: interests,
              dropdownDecoratorProps: DropDownDecoratorProps(
                dropdownSearchDecoration: InputDecoration(
                  labelText: "Interests",
                  hintText: "Search or enter new interest",
                ),
              ),
              popupProps: PopupPropsMultiSelection<String>.menu(
                showSearchBox: true,
                emptyBuilder: (context, searchEntry) {
                  return ListTile(
                    onTap: () => pageBloc.updateInterests(list.addWith(searchEntry)),
                    leading: const Icon(Icons.add),
                    title: Text(searchEntry),
                  );
                },
              ),
              items: list,
            );
          },
        );
      },
    );
  }
}

Additional context

Stacktrace

======== Exception caught by scheduler library =====================================================
The following assertion was thrown during a scheduler callback:
setState() called after dispose(): FormFieldState#27115(lifecycle state: defunct, not mounted)

This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree (e.g., whose parent widget no longer includes the widget in its build). This error can occur when code calls setState() from a timer or an animation callback.

The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the "mounted" property of this object before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object is retaining a reference to this State object after it has been removed from the tree. To avoid memory leaks, consider breaking the reference to this object during dispose().

When the exception was thrown, this was the stack: 
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/errors.dart 266:49      throw_
packages/flutter/src/widgets/framework.dart 1078:9                                <fn>
packages/flutter/src/widgets/framework.dart 1112:14                               setState
packages/flutter/src/widgets/form.dart 409:5                                      didChange
packages/dropdown_search/dropdown_search.dart 385:18                              <fn>
packages/flutter/src/scheduler/binding.dart 1175:15                               [_invokeFrameCallback]
packages/flutter/src/scheduler/binding.dart 1113:9                                handleDrawFrame
packages/flutter/src/scheduler/binding.dart 1015:5                                [_handleDrawFrame]
lib/_engine/engine/platform_dispatcher.dart 1168:13                               invoke
lib/_engine/engine/platform_dispatcher.dart 219:5                                 invokeOnDrawFrame
lib/_engine/engine/initialization.dart 195:45                                     <fn>
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 334:14  _checkAndCall
dart-sdk/lib/_internal/js_dev_runtime/private/ddc_runtime/operations.dart 339:39  dcall

```====================================================================================================

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions