如何防止setState((){});扑扑?

时间:2020-08-24 15:49:13

标签: flutter-layout setstate flutter-form-builder

`setState((){ //在这里如何防止设置状态 等级= aaa; });

onChanged: (aaa) {
   setState(() {
    rating = aaa;
   });
},`

1 个答案:

答案 0 :(得分:1)

您可以使用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(......)
)