Avijit
Avijit

Reputation: 697

Not able to select item of Flutter Dropdownbutton

I was trying to implement dropdownbutton in a flutter project.Dropdownbutton list will be populated from api. I have fetched the list from api.

But when I select an item, it is not showing. The error message is given below

The following assertion was thrown building FutureBuilder<List<Categories>>(dirty, state: _FutureBuilderState<List<Categories>>#09534):
There should be exactly one item with [DropdownButton]'s value: Instance of 'Categories'. 
Either zero or 2 or more [DropdownMenuItem]s were detected with the same value
'package:flutter/src/material/dropdown.dart':
Failed assertion: line 805 pos 15: 'items == null  items.isEmpty  value == null ||
              items.where((DropdownMenuItem<T> item) {
                return item.value == value;
              }).length == 1'

I am also adding the dropdownbutton code on the Pastebin.

https://pastebin.com/ReJPXN6D

Upvotes: 0

Views: 1234

Answers (1)

Jaydeep chatrola
Jaydeep chatrola

Reputation: 2701

i think in your case value: _currentItemSelected might be null

also you should manage FutureBuilder snapshotstate and error

String _currentItemSelected = "";

FutureBuilder<List<Categories>>(
future: _fetchCats(),
builder: (BuildContext context,
AsyncSnapshot<List<Categories>> snapshot) {
                      if (snapshot.connectionState == ConnectionState.waiting) {
                    return Center(
                      child: CircularProgressIndicator(),
                    );
                  } else if (snapshot.hasError) {
                    return Center(
                      child: Text(
                        snapshot.error,
                      ),
                    );
                  } else {

                   return  DropdownButton<String>(
                            isExpanded: true,
                            items: snapshot.data?.map((Categories value) {
                              return new DropdownMenuItem<String>(
                                value: value.name,
                                child: new Text(value.name, style: new TextStyle(fontSize: 16.0),),
                              );
                            })?.toList() ?? [],

                          onChanged: (String value) {
                            //field.didChange(value.name);

                            setState(() {
                              print(value);
                              this._currentItemSelected = value;
                            });
                          },
                            value: _currentItemSelected == "" ? _currentItemSelected = snapshot.data.first.name : _currentItemSelected,,
                            hint: Text('Event Category',
                              style: TextStyle(
                                fontSize: 15,
                                color: Colors.black,
                              ),
                            ),
                            underline: Container(
                              decoration: const BoxDecoration(
                                  border: Border(bottom: BorderSide(color: Colors.grey))
                              ),
                            ),
                            style: TextStyle(
                              fontSize: 15,
                              color: Color(0xffF9B538),
                            ),
                          );
                        })

                       }

hope it helps..

Upvotes: 3

Related Questions