Multiple values in DropdownPreference exception
I followed the example code to create a preference screen, however when starting the app and navigating to the settings screen I get the following exception:
The following assertion was thrown building DropdownPreference<String>(dirty, state:
_DropdownPreferenceState<String>):
There should be exactly one item with [DropdownButton]'s value: England.
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
This happens mostly during hot-reload, the preference widget seems to "remember" previous states and build upon them.
My code:
main() async {
WidgetsFlutterBinding.ensureInitialized();
await PrefService.init(prefix: 'pref_');
PrefService.setDefaultValues({});
runApp(MainApp());
}
class Settings extends StatelessWidget {
static const routeName = '/settings';
...
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(S.of(context).Settings),
),
body: Column(
children: [
PreferenceTitle(S.of(context).SettingsLocale),
DropdownPreference(
S.of(context).SettingsLocaleCountry,
'country',
defaultVal: countryList[0],
values: countryList,
onChange: (value) {
updateLocale(context, value);
},
),
],
),