[英]Flutter setState() not refreshing widget when called from stateful child
[英]Flutter Refreshing Stateful widget
我有一個有狀態的 class 顯示列表如下:
Container(
margin: const EdgeInsets.fromLTRB(50.0, 10.0, 15.0, 10.0),
height: 190,
width: 100,
child: ListView.builder(
itemCount: gameManager.yourScoresList.length,
itemBuilder: (context, index) {
return ListTile(
visualDensity: VisualDensity(
vertical: VisualDensity.minimumDensity),
title: gameManager.yourScoresList[index],
);
}))
但是,當我從另一個 class (GameManager) 清除 yourScoresList.clear() 時,ListView 不會被清除(即繼續顯示現有),直到我 select 中的另一個小部件在實現 setState() 的有狀態 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 中。 我試圖將 ListView 包裝在 ValueListenableBuilder 中,但我無法讓它工作。 任何建議都非常感激。
正如克里斯托弗摩爾所說,你必須在 class 中設置狀態,比如,
void clearbox(){
setState((){
yourScoresList.clear();
});
如果您的另一個 class 中沒有 StateFulWidget,請執行此操作,
在您的 StateFulWidget 中,(當前的 class,其中您有 listView)
添加此 function,
void clear(){
setState((){
gameManager.yourScoresList.clear();
});
}
調用這個 function,無論何時你想清除列表,你甚至可以從另一個 class 中調用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.