3

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
  • 869
  • 1
  • 12
  • 19

2 Answers2

11

Try

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

  },
  );
Josteve
  • 11,459
  • 1
  • 23
  • 35
  • 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 Sep 01 '20 at 15:36
  • 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. – ombiro Sep 01 '20 at 18:16
0

If you want to dismiss the alert dialog, then you should pop from it right when they navigate to the next screen. To rebuild the home screen once you click back, you can use a callback, so once you press the back button it will fire the callback that's declared in the home screen.

Home Screen

void rebuildPage() {
    setState(() {});
  }

onPressed: () {
    Navigator.pop(context);
    Navigator.of(context)
        .push(new MaterialPageRoute(builder: (context) => SettingsScreen(onPressed: rebuildPage)));
  },

Settings Screen

 final Function onPressed;
  
 SettingsScreen({ this.onPressed });

 AppBar(
        leading: IconButton(
          icon: Icon(Icons.arrow_back),
          onPressed: () {
            Navigator.pop(context);
            onPressed();
          },
        )
      ),
Unbreachable
  • 724
  • 8
  • 19