Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 如何从有状态小部件的实例访问其内部的变量_Flutter_Statefulwidget - Fatal编程技术网

Flutter 如何从有状态小部件的实例访问其内部的变量

Flutter 如何从有状态小部件的实例访问其内部的变量,flutter,statefulwidget,Flutter,Statefulwidget,我使用一个stateful类,它返回DropdownButton小部件,我需要访问其中的一个变量,该变量不是final,可以通过其对象访问。 这是我的密码: class ListItemHelper extends StatefulWidget { String name = ''; @override _ListItemHelperState createState() => _ListItemHelperState(); } class _ListItemHel

我使用一个
stateful类
,它返回
DropdownButton
小部件,我需要访问其中的一个变量,该变量不是final,可以通过其
对象
访问。 这是我的密码:

    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;
}
类ListItemHelper扩展StatefulWidget{
字符串名=“”;
@凌驾
_ListItemHelperState createState()=>\u ListItemHelperState();
}
类_ListItemHelperState扩展状态{
列表_nloc=[
const ListItem(1,“喀布尔”),
常量列表项(2,“Mazar”)
];
列表项_nlocSelectedItem;
ListItem get listI=>\u nlocSelectedItem;
@凌驾
小部件构建(构建上下文){
返回下拉按钮(
值:_nlocSelectedItem,
onChanged:(ListItem newValue){
设置状态(){
_nlocSelectedItem=新值;
widget.name=newValue.name;
印刷品(
'这是菜单idxx:${newValue.id},标题是${newValue.name}';
});
},
items:_nloc.map((ListItem ListItem){
返回新的DropdownMenuItem(
值:listItem,
儿童:新文本(
listItem.name,
样式:新文本样式(颜色:Colors.black),
),
);
}).toList(),
);
}
}
类列表项{
const ListItem(this.id,this.name);
最后的字符串名;
最终int id;
}
为了清楚起见,我需要通过类的对象访问变量名