Search code examples
fluttersetstatedropdownbutton

"The declaration 'setState' isn't referenced." warning for DropdownButton within StatefulWidget


I have a stateful widget in which I put my DropdownButton. Inside DropdownButton's onChange event, I have a setState but it keeps warning me that the declaration 'setState' isn't referenced. And it's actually not called whenever onChange event is fired.

Any suggestion would be highly appreciated.

Please see the code snippet below for more detailed.

class TypeSetup extends StatefulWidget {
  const TypeSetup({Key? key}) : super(key: key);

  @override
  _TypeSetupState createState() => _TypeSetupState();
}

class _TypeSetupState extends State<TypeSetup> {
  var _selectedType;
  List<Type>? _types;

  @override
  void initState() {
    super.initState();

    loadTypeJson();
  }

  Future loadTypeJson() async {
    String typeJson = await DefaultAssetBundle.of(context)
        .loadString("assets/data/types.json");

    Iterable typeIter = json.decode(typeJson)["accommodation-types"];

    setState(() {
      _types = List<Type>.from(typeIter.map((model) => Type.fromJson(model)));
    });
  }

  @override
  Widget build(BuildContext context) {
    final typeList = _types?.map((model) {
      return new DropdownMenuItem<String>(
        value: model.type,`enter code here`
        child: new Text(model.name),
      );
    }).toList();

    return Container(
      padding: EdgeInsets.all(DefaultPalette.defaultPadding),
      decoration: BoxDecoration(
        color: DefaultPalette.secondaryColor,
        borderRadius: const BorderRadius.all(Radius.circular(10)),
      ),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(
          "Set up",
          style: Theme.of(context).textTheme.subtitle1,
        ),
        SizedBox(
            width: double.infinity,
            child: Center(
                child: Padding(
                    padding: const EdgeInsets.all(16.0),
                    child: Container(
                      padding: EdgeInsets.only(left: 16, right: 16),
                      decoration: BoxDecoration(
                          border: Border.all(color: Colors.grey, width: 1),
                          borderRadius: BorderRadius.circular(13)),
                      child: DropdownButton(
                        hint: Text('Select  Type'),
                        dropdownColor: Colors.white,
                        icon: Icon(Icons.arrow_drop_down),
                        iconSize: 30,
                        isExpanded: true,
                        underline: SizedBox(),
                        style: TextStyle(color: Colors.black, fontSize: 22),
                        value: _selectedType,
                        onChanged: (newValue) {
                          setState() {
                            _selectedType = newValue;
                            print("Selected type changed.");
                          }
                        },
                        items: typeList,
                      ),
                    )
                )
            )
          )
      ]),
    );
  }
}

Solution

  • You need to call setState like this

    setState(() {
      _selectedType = newValue;
    });