2

I use a stateful class that returns DropdownButton widget for me, I need to access a variable in it that is not final and can be accessed through its object. here is my code:

    class ListItemHelper extends StatefulWidget {
  String name = '';

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

class _ListItemHelperState extends State<ListItemHelper> {
  List<ListItem> _nloc = <ListItem>[
    const ListItem(1, 'Kabul'),
    const ListItem(2, 'Mazar')
  ];
  ListItem _nlocSelectedItem;

  ListItem get listI => _nlocSelectedItem;

  @override
  Widget build(BuildContext context) {
    return DropdownButton<ListItem>(
      value: _nlocSelectedItem,
      onChanged: (ListItem newValue) {
        setState(() {
          _nlocSelectedItem = newValue;
          widget.name = newValue.name;
          print(
              'this is the menu idxx: ${newValue.id} and the title is ${newValue.name}');
        });
      },
      items: _nloc.map((ListItem listItem) {
        return new DropdownMenuItem<ListItem>(
          value: listItem,
          child: new Text(
            listItem.name,
            style: new TextStyle(color: Colors.black),
          ),
        );
      }).toList(),
    );
  }
}

class ListItem {
  const ListItem(this.id, this.name);

  final String name;
  final int id;
}

to make it clear I need the variable name to be accessed through the object of the class.

icy
  • 1,468
  • 3
  • 16
  • 36

0 Answers0