` setState(() { //这里如何防止设置状态 rating = aaa; });
onChanged: (aaa) {
setState(() {
rating = aaa;
});
},`
` setState(() { //这里如何防止设置状态 rating = aaa; });
onChanged: (aaa) {
setState(() {
rating = aaa;
});
},`
您可以使用 ValueListenableBuilder 代替 setState。例如:如果 rating 是 int 类型。因此,您可以创建一个 int 类型的 ValueNoifier 并监听它的变化。
ValueNotifier<int> rating = ValueNotifier<int>(0);
//......change the value of rating in the onChanged:
onChanged(aaa){
rating.value = aaa;
}
// .... Wrap your Widget with Listenable builder to listen to the change in rating.
ValueListenableBuilder(
valueListenable: rating,
builder : (context, value, child) => SomeWidget(......)
)