Home > Enterprise >  how to force build when ever a page shows up in flutter
how to force build when ever a page shows up in flutter

Time:01-17

Hello hope you guys are ok. the problem I'm facing is we have a main_page which leads to a page doing some changes on data which are show on the main page. after some process if the user touches back button and goes to main_page app loads it from stack and the data are not shown because it does not get rebuilt. I don't want to control back button because there are other pages which lead to data changing page and I also tried using valuelistenablebuilder but I don't know why it goes wild and gets into a screen refresh loop without even changing the valueListenable I used redux to manage the value. actual reason I want main page to rebuild is to call a method. why do I not call that method in second page is complicated and because i don't want to.

in conclusion I want main page to rebuild whenever it shows up even when it's read from the stack or even is there a way to tamper with stack of the pages without visual changes to the user.

CodePudding user response:

you need to use Shared preferences plugin https://pub.dev/packages/shared_preferences

CodePudding user response:

If I understood the question correctly

The flow should be:

Screen A

@override
void initState() {
 loadSomeDataFromDB();
 super.initState();
}

Screen B

@override
void initState() { *//INIT AS EXAMPLE*
 changeSomeDataFromDB();
 super.initState();
}

You can try this in Screen A

 onPressed: () async {
            bool? shouldLoadDataAgain = await 
            Navigator.of(context).push(PageRouteBuilder(
              pageBuilder: (context, animation, secondaryAnimation) =>
                  const ScreenB(),
            ));
            if(shouldLoadDataAgain!=null&&shouldLoadDataAgain ==true){
             loadSomeDataFromDB();
            }
          },

and this in Screen B when user press back button

  onPressed: () async {
            Navigator.of(context).pop(true)
          },
  • Related