0

I have a language selection dropdown for English and Malayalam
After selecting language, selection stays blank. Can only see the hint text.

I am not getting any errors. Flutter doctor is fine

              Container(
                height: 250,
                width: double.infinity,
                child: Column(
                  children: <Widget>[
                    Flexible(
                      flex: 0,
                      child: Padding(
                        padding: EdgeInsets.only(left: 10, right: 10, top: 15),
                        child: Container(
                          width: double.infinity,
                          height: 45,
                          decoration: BoxDecoration(
                              color: Colors.white,
                              borderRadius: BorderRadius.circular(5),
                              border: Border.all(color: Colors.blueGrey)
                          ),
                          child:  DropdownButton(
                            isExpanded: true,
                            itemHeight: 50,
                            icon: Icon(Icons.arrow_drop_down),
                            iconSize: 40,
                            underline: SizedBox(),
                            hint: Padding(
                              padding: const EdgeInsets.all(8.0),
                              child: Text(AppLocalization.of(context).getTranslatedValues('selectlang'),style: TextStyle(fontSize: 15,color: Colors.black54),),
                            ),
                            onChanged: (Language language) {
                              _changeLanguage(language);
                              setState(() {
                                _selectedeLang = language;
                              });
                            },
                            items: Language.languageList()
                                .map<DropdownMenuItem<Language>>((lang) => DropdownMenuItem(
                              value: lang,

                              child: Row(
                                children: <Widget>[
                                  Center(child: Text((lang.name),style: TextStyle(fontSize: 15),))
                                ],
                              ),
                            ))
                                .toList(),
                          ),
                        ),
                      ),
                    ),
                    Flexible(
                      flex: 0,
                      child: Padding(padding: EdgeInsets.only(left: 8,top: 25,bottom: 8),
                        child: GestureDetector(
                          child:  Container(
                            height: 45,
                            width: 200,
                            child: Center(child: Text(AppLocalization.of(context).getTranslatedValues('submit'),style: TextStyle(color: Colors.white,fontSize: 12,fontWeight: FontWeight.bold),),),
                            decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(10),
                              color: Color.fromRGBO( 34, 83, 148,1),
                            ),
                          ),
                          onTap: (){
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => RegisterScreen(),
                              ),
                            );

                          },),
                      ),
                    ),
                  ],
                ),
              ) 

Onchange Function

 void _changeLanguage(Language language) {
    Locale _temp;
    switch (language.languageCode) {
      case 'en':
        _temp = Locale(language.languageCode, 'US');
        break;
      case 'ml':
        _temp = Locale(language.languageCode, 'IN');
        break;
      default:
        _temp = Locale(language.languageCode, 'US');
    }

    MyApp.setLocale(context, _temp);
  }
Adelina
  • 10,915
  • 1
  • 38
  • 46
Abhijith
  • 2,227
  • 2
  • 15
  • 39

1 Answers1

2

You need to add value: dropdownValue to your DropdownButton;

    DropdownButton(value:_selectedeLang, ...

Also update your Language class:

class Language {
  final int id;
  final String name;
  final String flag;
  final String languageCode;
  Language(this.id, this.name, this.flag, this.languageCode);

// add these operators, so could compare two Language instances
    @override
  bool operator ==(Object other) => other is Language && other.id == id;

    @override
  int get hashCode => id.hashCode;

  static List<Language> languageList() {
    return <Language>[
      Language(1, 'English', '', 'en'),
      Language(2, 'മലയാളം', '', 'ml'),
    ];
  }
}
Adelina
  • 10,915
  • 1
  • 38
  • 46
  • Throws error like this after i select the dropdown item `There should be exactly one item with [DropdownButton]'s value: Instance of 'Language'. Either zero or 2 or more [DropdownMenuItem]s were detected with the same value 'package:flutter/src/material/dropdown.dart': Failed assertion: line 827 pos 15: 'items == null || items.isEmpty || value == null || items.where((DropdownMenuItem item) { return item.value == value; }).length == 1'` @Nuts – Abhijith Jun 19 '20 at 13:26
  • What is your initial `_selectedeLang ` value? This error normally happens if selected value is not in the dropdown – Adelina Jun 19 '20 at 13:32
  • i did as `Language _selectedeLang;` @Nuts – Abhijith Jun 19 '20 at 13:35
  • How does your Language class look like? You might need to update it, so two instances could be compared for example with: https://pub.dev/packages/equatable – Adelina Jun 19 '20 at 13:37
  • `class Language{ final int id; final String name; final String flag; final String languageCode; Language(this.id,this.name,this.flag,this.languageCode); static List languageList(){ return [ Language(1,'English','','en'), Language(2,'മലയാളം','','ml'), ]; } }` This is my model look like,Sorry for adding model here,i couldn't add to question, i having problem it is mostly of code, @Nuts – Abhijith Jun 19 '20 at 14:02
  • Try this out https://stackoverflow.com/questions/60510150/flutter-there-should-be-exactly-one-item-with-dropdownbuttons-value/62471836#62471836 – Adelina Jun 19 '20 at 14:03
  • let me look into that.. also please try to look into the Langauge class @Nuts – Abhijith Jun 19 '20 at 14:05