Home > Software engineering >  Flutter Alert once per session
Flutter Alert once per session

Time:03-24

I have a payment due alert in my application. It opens up on init of homepage. What my objective is to open the alert only once when the app opens up. I don't want to show that alert again when the user comeback to home screen. How can I achieve this?.

CodePudding user response:

You can make a separate class and create flag there like this:

class Management {
  static bool dialogAppeared = false;
}

and your initState would look something like this:

@override
  void initState() {
    WidgetsBinding.instance?.addPostFrameCallback((_) => openDialog());
    super.initState();
  }

  void openDialog() {
    if (Management.dialogAppeared == false) {
      showDialog(context: context, builder: (context) => YourDialog())
          .then((value) => Management.dialogAppeared = true);
    }
  }

CodePudding user response:

I created this enter image description here

You'll be persisting the value in a Provided service, where the value will be persisted regardless of whether the widget rebuilds, since the state has been lifted and separated from the UI building logic. Check out the Gist above and run it on DartPad and you'll see.

  • Related