我有一個非常簡單的使用顫振鉤子的例子:
class PositionedTiles extends HookWidget {
@override
Widget build(BuildContext context) {
final counterList = useState<List<int>>([1,2,3,4,5]);
return Scaffold(
body: Text("Counter: ${counterList.value}"),
floatingActionButton: FloatingActionButton(
onPressed: () => counterList.value.removeAt(0),
child: const Icon(Icons.sentiment_very_satisfied)),
);
);
}
因此,單擊floatingActionButton列印串列 ( counterList) 后不會更改。我的結論是沒有呼叫重建。
為什么 hook 沒有看到串列已更改?
如何在呼叫counterList后表示串列 () 已更改onPressed以進行重建?
uj5u.com熱心網友回復:
要更新狀態,您需要重新分配串列。
onPressed: () {
counterList.value = [...counterList.value..removeAt(0)];
log("${counterList.value}");
},
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/512882.html
標籤:扑镖颤振小部件扑钩
