suranju
suranju

Reputation: 25

Flutter DropdownMenu widget's TextEditingController disposed when closing dialog

I have used DropdownMenu widget inside a Dialog widget, which again is inside a StatefulBuilder, as shown below:

DropdownMenu(
   controller: categoryIdController,
   hintText: 'Select a Category',
   onSelected: (value) => setState(() {
     pageCategoryId = value ?? '';
   }),
   dropdownMenuEntries: <
       DropdownMenuEntry<String>>[
     ...List.generate(
         categoryList.length,
         (index) => DropdownMenuEntry<String>(
             value: categoryList[index].id,
             label: categoryList[index].name))
   ],
 )

When i open the dialog, the first time it works. As I close and open the dialog again, I get this error: A TextEditingController was used after being disposed. Once you have called dispose() on a TextEditingController, it can no longer be used.

The following assertion was thrown building TextField(controller:
TextEditingController#929b9(TextEditingValue(text: ┤├, selection: TextSelection.invalid, composing:
TextRange(start: -1, end: -1))), focusNode: FocusNode#3d4e3, decoration: InputDecoration(hintText:
"Select a Category", floatingLabelBehavior: FloatingLabelBehavior.auto, floatingLabelAlignment:
FloatingLabelAlignment.start, suffixIcon: Padding(padding: EdgeInsets(4.0, 0.0, 4.0, 0.0)), filled:
true, border: OutlineInputBorder(), alignLabelWithHint: false), style: TextStyle(debugLabel:
(englishLike labelLarge 2021).merge((blackHelsinki labelLarge).apply), inherit: false, color:
Color(0xff1a1c19), family: Roboto, familyFallback: [Ubuntu, Cantarell, DejaVu Sans, Liberation Sans,
Arial], size: 14.0, weight: 500, letterSpacing: 0.1, baseline: alphabetic, height: 1.4x,
leadingDistribution: even, decoration: Color(0xff1a1c19) TextDecoration.none), dependencies:
[DefaultSelectionStyle, MediaQuery, UnmanagedRestorationScope, _InheritedTheme,
_LocalizationsScope-[GlobalKey#22766]], state: _TextFieldState#c4291):
A TextEditingController was used after being disposed.
Once you have called dispose() on a TextEditingController, it can no longer be used.
The relevant error-causing widget was:
  DropdownMenu<String>

I dont understand what causes the dispose of the TextEditingController. Any ideas please.

Upvotes: 0

Views: 454

Answers (1)

suranju
suranju

Reputation: 25

I was able to fix this problem. Had to remove the controller from DropdownMenu. The documentation says that the widget creates its own controller if not assigned.

Upvotes: 0

Related Questions