I have Page called ProductSearch and ProductGride if I navigate from search page to gride page isLoading is not updating from the future function of the viewmodel of grid page.
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: [
]
);
}
}
CodePudding user response:
Put isLoading = false into the setState method after then or else you can call that in .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;})}
CodePudding user response:
The isLoading
state is not changing. You need to change the state of it.
Try as follows
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});
}
);
}
CodePudding user response:
You can use setstate()
to update the state. After navigating to the other page setstate() will help you to react accordingly.
Wrap isLoading with setstate()