DropdownButtonFormField not getting rebuilt
In Flutter version 1.17.2, that DropdownButtonFormField
bug was fixed, so be sure to upgrade.
Github issue: https://github.com/flutter/flutter/issues/56898
Fixed in version 1.17.2: https://github.com/flutter/flutter/wiki/Hotfixes-to-the-Stable-Channel#1172-may-28-2020
Wilson
Updated on December 20, 2022Comments
-
Wilson over 1 year
I'm trying to update the selected value programmatically.
I've used various method, including Consumer etc., and have made sure that the value is updated and the widget is called when the value changes, however, DropdownButtonFormField never got rebuilt with the latest value.
Currently I'm wrapping the DropdownButtonFormField in a StreamBuilder, which supposedly, should get rebuild whenever there's a new event sent through the stream. This is my code:
Declaration
final StreamController<String> _raceStreamController = new StreamController<String>();
DropdownButtonFormField
return StreamBuilder<String>( stream: _raceStreamController.stream, builder: (BuildContext context, AsyncSnapshot<String> snapshot) { return new DropdownButtonFormField<String>( value: snapshot.data, hint: new Text(hint, textAlign: TextAlign.center), isExpanded: true, items: items.map((String value) { return new DropdownMenuItem<String>( child: new Text(value), value: value ); }).toList(), validator: (value) => value == null ? 'field required' : null, onChanged: (value) {} // no use for now, ); });
Push data
onFocusChange: (focus) async { if (!focus) { try{ await userBloc.searchUser(controller.text.toUpperCase()); _raceStreamController.sink.add(userBloc.user.race); } catch(e) { if (e.toString() == ERROR_UNAUTHORISED) navigateToRoot(context); } } }
I've tried to remove as much redundant code as possible.
Thank you.
-
Viren V Varasadiya about 4 yearsdid you make sure value you are adding in stream is available in item list ?
-
Wilson about 4 years@VirenVVarasadiya Oh ya, I forgot to mention that too. Yes, the values (string) added into the stream sink, is one of the items under DropdownButtonFormField, I've put break points and ensure that is correct too. If I set the default value, to one of the items, it'll work, DropdownButtonFormField will show that item selected. But if the default value is null or not provided, and I use either a provider/consumer, or like the code shown, a streamcontroller/streambuilder method, when the value provided is not null anymore, it just doesn't reflect it.
-
Wilson about 4 years@VirenVVarasadiya One more thing, if the value is not one of the items, errors will be picked up by the debug console, but it didn't.
-
-
Wilson almost 4 yearsI have tested and confirmed it is working now, thanks a lot.