skip to Main Content

I have this code

void _setScreen(String identifiers,int id) {
  if (identifiers == _get_Current_Screen_Identifier()) {
    Navigator.of(context).pop();
  } else {
    Navigator.of(context).pop();
    if (identifiers == 'page1') {
      Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const Page1()));
    }
    if (identifiers == 'page2') {
      Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const Page2()));
    }
  }
}

The first screen to show when I open the application is Page1. In Page2 I can add some kind of value in a table of the DB (sqlite). In Page1, after any kind of operation, I do setState to see if these values are updated/added/delete, but when I returned from Page2 to Page1 the value doesn’t update, until I do some operation that calls setState.

Is there a way to do that?

I’ve tried

void _setScreen(String identifiers,int id) {
  if (identifiers == _get_Current_Screen_Identifier()) {
    Navigator.of(context).pop();
  } else {
    Navigator.of(context).pop();
    if (identifiers == 'page1') {
      Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const Page1()));
    }
    if (identifiers == 'page2') {
      Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const Page2())).then(
        (value) {
        setState(() {
        });
      });
    }
  }
}

But it still doesn’t work.

2

Answers


  1. If you go to Page2 and do some actions, then come back to Page1 and want to update your values you can use the await method
    you can do something like this:

    IMPORTANT Do NOT forget to add async in your function.

    void _setScreen(String identifiers,int id) async{}
    

    Use Below Code:

    await Navigator.of(context).push(MaterialPageRoute(builder: (ctx) => const Page2()));
    setState((){});
    

    Now call setState((){});
    and any other action you b
    need to apply

    For instance:

    onTap: () async {
     await Navigator.of(context).push(MaterialPageRoute(
     builder: (ctx) => const Page2()));                                  
    //////Call SetState or any other action...                            
    print("do Actions"); }
    

    Let me know if your problem is solved or not.
    Happy Coding 🙂

    Login or Signup to reply.
  2. Yes, You can

    Page 1

     Future<void> pushToSettingScreen(BuildContext context) async {
        //
        final result = await Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => const yourScreenName(),
          ),
        );
    
        if (!mounted) return;
        //
        if (result == 'reloadMe') {
          if (kDebugMode) {
            print(result);
          }
    
            setState(() {
             screenLoader = true;
            }); // your whole screen reload automatically
        }
      }
    

    In Page 2

    • Use Navigator.pop(context,'reloadMe') instead of Navigator.pop(context)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search