0

When I added the 'value' parameter to the DropdownButton my app crashed with the exception I mentioned in the title. When I'm removing the 'value' parameter it workd but showing a hint and when I select a value from the list its not really selecten I mean I can't see I selected it

  Widget build(BuildContext context) {
    String dropdownValue = 'Categories';
    return Scaffold(
        appBar: AppBar(
          title: Text(
            'Podium',
            style: TextStyle(fontSize: 18),
          ),
          backgroundColor: Colors.black54,
        ),
        backgroundColor: Colors.grey,
        body: FutureBuilder<Competition>(
          future: widget.jsonData.fetchComp(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {
              return Center(
                  child: Column(
                children: <Widget>[
                  Text(
                    snapshot.data.compName,
                    style: TextStyle(fontSize: 18),
                  ),
                  DropdownButton<String>(
                    items:
                        snapshot.data.categoriesNames.map((dropdownStringItem) {
                      return DropdownMenuItem<String>(
                        value: dropdownStringItem,
                        child: Text(dropdownStringItem),
                      );
                    }).toList(),
                    onChanged: (newItemSelected) {
                      setState(() {
                        dropdownValue = newItemSelected;
                      });
                    },
                    value: dropdownValue,
                    elevation: 16,
                    icon: Icon(Icons.arrow_downward),
                    iconSize: 24,
                    style: TextStyle(color: Colors.black54),
                    underline: Container(
                      height: 2,
                      color: Colors.deepPurpleAccent,
                    ),
                  )
                ],
              ));
            } else if (snapshot.hasError) {
              return Text('${snapshot.error}');
            }
            return Center(
              child: CircularProgressIndicator(),
            );
          },
        ));
  }
almogbb
  • 29
  • 7
  • Possible duplicate of [How to implement drop down list in flutter?](https://stackoverflow.com/questions/49273157/how-to-implement-drop-down-list-in-flutter) – cegas Oct 18 '19 at 11:29

1 Answers1

0
Widget build(BuildContext context) {

String dropdownValue = null;
return Scaffold(
    appBar: AppBar(
      title: Text(
        'Podium',
        style: TextStyle(fontSize: 18),
      ),
      backgroundColor: Colors.black54,
    ),
    backgroundColor: Colors.grey,
    body: FutureBuilder<Competition>(
      future: widget.jsonData.fetchComp(),
      builder: (context, snapshot) {
        if (snapshot.hasData) {
          return Center(
              child: Column(
            children: <Widget>[
              Text(
                snapshot.data.compName,
                style: TextStyle(fontSize: 18),
              ),
              DropdownButton<String>(
                items:
                    snapshot.data.categoriesNames.map((dropdownStringItem) {
                  return DropdownMenuItem<String>(
                    value: dropdownStringItem,
                    child: Text(dropdownStringItem),
                  );
                }).toList(),
                onChanged: (newItemSelected) {
                  setState(() {
                    dropdownValue = newItemSelected;
                  });
                },
                value: dropdownValue,
                elevation: 16,
                icon: Icon(Icons.arrow_downward),
                iconSize: 24,
                style: TextStyle(color: Colors.black54),
                underline: Container(
                  height: 2,
                  color: Colors.deepPurpleAccent,
                ),
              )
            ],
          ));
        } else if (snapshot.hasError) {
          return Text('${snapshot.error}');
        }
        return Center(
          child: CircularProgressIndicator(),
        );
      },
    ));
}

dropdown value should be null at starting or item of "items" properties.

e.g.
   String dropdownValue = null;
Sonu Saini
  • 1,814
  • 9
  • 16