Drop down menu 找不到方法';设置状态';小部件内
我有一个小部件,稍后在我的主脚手架文件中调用。这个小部件包含一个下拉菜单,但在选择另一个值时,我无法更改状态。该字段未更新,我收到错误消息“error:Method not found:“setState”。 设置状态((){' ^^^^^^^^ 我已经更新了setState方法并从中删除了代码,但它仍然说找不到该方法Drop down menu 找不到方法';设置状态';小部件内,drop-down-menu,flutter,dropdown,setstate,Drop Down Menu,Flutter,Dropdown,Setstate,我有一个小部件,稍后在我的主脚手架文件中调用。这个小部件包含一个下拉菜单,但在选择另一个值时,我无法更改状态。该字段未更新,我收到错误消息“error:Method not found:“setState”。 设置状态((){' ^^^^^^^^ 我已经更新了setState方法并从中删除了代码,但它仍然说找不到该方法 child: DropdownButton( hint: Text('Medical'), value:
child: DropdownButton(
hint: Text('Medical'),
value: _selectedCustomerType,
onChanged: (newValue) {
setState(() {
_selectedCustomerType = newValue;
});
},
items: _customerType.map((cusType) {
print(cusType);
return DropdownMenuItem(
child: Text(cusType),
value: cusType,
);
}).toList(),
),
我需要能够更新值并在选择新值时显示它。SetState在主方法中不可访问,在函数中也不可访问,为了使其可访问,您需要创建一个有状态类,并且正好在State类中,因为实际上您的小部件是一个有状态类:它每次都会更改状态用户创建事件..设置状态在主方法内部不可访问,在函数内部也不可访问,要使其可访问,您需要创建一个有状态类,并且正好在状态类中,因为实际上您的小部件是一个有状态类:它在用户每次创建事件时都会更改其状态..您不能在因此,您应该将DropdownButton包装在StatefulWidget中,例如:
class StatefulDropdownButton extends StatefulWidget {
final List<String> _customerType;
StatefulDropdownButton(this._customerType);
@override
State<StatefulWidget> createState() => DropdownButtonState();
}
class DropdownButtonState extends State<StatefulDropdownButton> {
String _selectedCustomerType;
@override
Widget build(BuildContext context) {
return DropdownButton(
hint: Text('Medical'),
value: _selectedCustomerType,
onChanged: (newValue) {
setState(() {
_selectedCustomerType = newValue;
});
},
items: widget._customerType.map((cusType) {
print(cusType);
return DropdownMenuItem(
child: Text(cusType),
value: cusType,
);
}).toList(),
);
}
}
class StatefulDropdownButton扩展StatefulWidget{
最终列表_客户类型;
StatefulDropdownButton(此.\u customerType);
@凌驾
状态createState()=>DropdownButtonState();
}
类DropdownButtonState扩展了状态{
字符串\u selectedCustomerType;
@凌驾
小部件构建(构建上下文){
返回下拉按钮(
提示:文本('Medical'),
值:\ u selectedCustomerType,
一旦更改:(newValue){
设置状态(){
_selectedCustomerType=newValue;
});
},
条目:widget.\u customerType.map((cusType){
打印(客户类型);
返回下拉菜单项(
子项:文本(cusType),
值:cusType,
);
}).toList(),
);
}
}
您不能在外部使用setState,因此您应该将下拉按钮包装在StatefulWidget中,例如:
class StatefulDropdownButton extends StatefulWidget {
final List<String> _customerType;
StatefulDropdownButton(this._customerType);
@override
State<StatefulWidget> createState() => DropdownButtonState();
}
class DropdownButtonState extends State<StatefulDropdownButton> {
String _selectedCustomerType;
@override
Widget build(BuildContext context) {
return DropdownButton(
hint: Text('Medical'),
value: _selectedCustomerType,
onChanged: (newValue) {
setState(() {
_selectedCustomerType = newValue;
});
},
items: widget._customerType.map((cusType) {
print(cusType);
return DropdownMenuItem(
child: Text(cusType),
value: cusType,
);
}).toList(),
);
}
}
class StatefulDropdownButton扩展StatefulWidget{
最终列表_客户类型;
StatefulDropdownButton(此.\u customerType);
@凌驾
状态createState()=>DropdownButtonState();
}
类DropdownButtonState扩展了状态{
字符串\u selectedCustomerType;
@凌驾
小部件构建(构建上下文){
返回下拉按钮(
提示:文本('Medical'),
值:\ u selectedCustomerType,
一旦更改:(newValue){
设置状态(){
_selectedCustomerType=newValue;
});
},
条目:widget.\u customerType.map((cusType){
打印(客户类型);
返回下拉菜单项(
子项:文本(cusType),
值:cusType,
);
}).toList(),
);
}
}
add your full code add your full code我无法捕获该类。这是一个由主小部件调用的小部件。SetState在主方法中不可访问,在函数中也不可访问。要使其可访问,您需要创建一个有状态类,并且正好在状态类中,因为实际上您的小部件是一个有状态类:i每次用户做出事件时,t都会更改其状态。我无法捕获该类。这是一个由主小部件调用的小部件。SetState在主方法中不可访问,在函数中也不可访问。要使其可访问,您需要创建一个有状态类,并且正好在状态类中,因为实际上您的小部件是有状态的l类:每当用户创建事件时,它都会更改其状态。我刚刚尝试了它,它工作了。我尝试在有状态小部件之外使用设置状态。我刚刚尝试了它,它工作了。我尝试在有状态小部件之外使用设置状态。