Close AlertDialog on Navigating back from second screen and refresh page flutter

5,679

Try

  Widget updateProfileButton = FlatButton(
  child: Text("Update",
  ),
  onPressed: () async {
    Navigator.pop(context);
    await Navigator.of(context)
        .push(new MaterialPageRoute(builder: (context) => SettingsScreen()));
    setState((){});

  },
  );
Share:
5,679
ombiro
Author by

ombiro

An Android, .Net (C#) Mobile programmer, Software Architect, Software Product Designer and Business man (http://www.backtude.com). I love hacks. I hate horror movies... I also hate movie series.

Updated on December 23, 2022

Comments

  • ombiro
    ombiro over 1 year

    I have a method which check if the current users location is set, if not, open an alert dialog with two buttons: Cancel and Update Profile. the onPressed of the Update Profile button navigates to a second screen with a form to update users location. The issue is: if the user clicks on the Update button and update the location, on clicking back button, the first page is displayed with the alert dialog still open. Expectation: on closing the back button and on updating the location, the back button opens the first screen and refreshes the whole page, getting the new location setting. See code: FIRST SCREEN (HOME SCREEN) @override void initState() { super.initState(); getUserDetails(); }

      updateProfileLocationPrompt(BuildContext context) {
      Widget cancelButton = FlatButton(
      child: Text("Cancel",
      ),
      onPressed: () {
        Navigator.of(context).pop(true);
      },
      );
      Widget updateProfileButton = FlatButton(
      child: Text("Update",
      ),
      onPressed: () {
        Navigator.of(context)
            .push(new MaterialPageRoute(builder: (context) => SettingsScreen()));
      },
      );
      AlertDialog alert = AlertDialog(
       title: Text("Update location",
       ),
       content: Text(
        "Please Update you location",
       ),
       actions: [
        cancelButton,
        updateProfileButton,
       ],
      );
    
       // show the dialog
      showDialog(
      context: context,
      builder: (BuildContext context) {
        return alert;
       },
       );
      }
    
        getUserDetails() async {
       var firebaseUser = FirebaseAuth.instance.currentUser;
    
       DocumentSnapshot value = await FirebaseFirestore.instance
        .collection(Str.USERS)
        .doc(firebaseUser.uid)
        .get();
      if (value.data()[Str.ADDRESS].toString() == null ||
        value.data()[Str.ADDRESS].toString() == "") {
      return updateProfileAndLocationPrompt(context);
      } else {
      setState(() {
        searchLocationAddress = value.data()[Str.ADDRESS].toString();//got from firebase
        getItems();
      });
     }
     return;
    }
    

    SECOND SCREEN - SETTINGS SCREEN Normal page with textfields for updating location to the firebase. The issue really: How do I navigate back to home screen, refresh home screen with the new data and reload the page thus not opening the dialog as the check on location once set should not open the dialog. Currently, even if i navigate from the dialog to the second screen, the alert dialog is still open, home screen is not refreshed with new data.

  • ombiro
    ombiro over 3 years
    This seems to work: On clicking back the dialog is dismissed, the home screen is not refreshed so that the check if location is set is also done once again. This will, if second screen has been updated, the home screen then will also get updated and the refresh will get latest data.
  • ombiro
    ombiro over 3 years
    This seems to work: Have edited - to auto update the home screen on changes on the second screen (changes made to firestore database) and re-render the home screen, I have used a listener on firestore changes and it worked perfectly. Thank you so much. You saved the day.