CricularProgress Bar没有改变isLoading变量的状态



我有一个名为ProductSearch和productgrid的页面,如果我从搜索页面导航到网格页面isLoading不是从网格页面视图模型的未来功能更新。

ProductGridePage

class ProductGridePage extends StatefulWidget {
@override
_ProductGridePageState createState()=> _ProductGridePageState();
}

class _ProductGridePageState extends State<ProductGridePage>{
int page_no=1;
int page_size=20;
bool isLoading=true;
void fetchData() async {
Provider.of<ProductgridVM>(context,listen: false).fetchProductGridList(
productcode: productcode,
branchname: branchname,
metalname: metalname,
purityname: purityname,
seriesname: seriesname,
producttypename: producttypename,
collectionname: collectionname,
categoriesname: categoriesname,
makingProcessname: makingProcessname,
page_no: page_no, page_size: page_size).then((value) => isLoading=false);
}
@override
void initState() {
super.initState();
_controller = new TextEditingController(text: '1');
fetchData();
}

@override
Widget build(BuildContext context) {
print(isLoading);
return isLoading?Center(child: CircularProgressIndicator())
: Scaffold(
body: Column(
children: [
]
);
}
}

将isLoading = false放入setState方法中,否则您可以在.whenComplete中调用它

void fetchData() async {
Provider.of<ProductgridVM>(context,listen: false).fetchProductGridList(
productcode: productcode,
branchname: branchname,
metalname: metalname,
purityname: purityname,
seriesname: seriesname,
producttypename: producttypename,
collectionname: collectionname,
categoriesname: categoriesname,
makingProcessname: makingProcessname,
page_no: page_no, page_size: page_size).then((value) => setState((){
isLoading=false;})}

isLoading状态未改变。你需要改变它的状态。尝试如下

void fetchData() async {
Provider.of<ProductgridVM>(context,listen: false).fetchProductGridList(
productcode: productcode,
branchname: branchname,
metalname: metalname,
purityname: purityname,
seriesname: seriesname,
producttypename: producttypename,
collectionname: collectionname,
categoriesname: categoriesname,
makingProcessname: makingProcessname,
page_no: page_no, page_size: page_size).then((value) {
/// You need to change the state like this
setState((){
isLoading=false});
}
);
}

可以使用setstate()来更新状态。导航到另一个页面后,setstate()将帮助您做出相应的反应。

Wrap isLoading withsetstate()

最新更新