1

I want to show the card on the main page once a week in my app.

Can anyone help me how can I do that?

I use Future.delayed in the init state and set duration "7 days" for it.

But I think it is wrong.

So how can I do that?

void initState() {
  Future.delayed(Duration(days: 7), () {
   showBottomSheet(context);
     }); 
 super.initState();
}
2
  • 2
    You need to use SharedPreferences or other local storage to handle this thing. Jun 7, 2022 at 10:35
  • 1
    Maybe store the starting date and check at the start of the app if it is 7 days after?
    – aoiTenshi
    Jun 7, 2022 at 10:37

3 Answers 3

2

You can do one thing

get weekday and set a condition on that like

DateTime date = DateTime.now();
print("weekday is ${date.weekday}");
if date.weekday == MONDAY {
      do task for Monday
}else {do task for other day.. }

this will very simple and easy away to trigger event on a specific day like sale on frieday or movie offer on Sunday..

0

Try to use Timer.run refer Timer,

@override
  void initState() {
    super.initState();
    Timer.run(() {
      Future.delayed(Duration(days: 7), () {
         showBottomSheet(context);//This function display after every 7 Days
      });
    });    
   }
2
  • Do you think that the user having the app in the foreground for 7 days straight for this to work is a realistic option?
    – nvoigt
    Jun 8, 2022 at 7:25
  • @nvoigt I think Jun 8, 2022 at 8:35
0

I want to show the card on the main page Once a week in my app. can anyone help me how can I do that?

You cannot do that without persisting data. Anything you do purely in your app will be gone when the user closes your app, moves it to the background of their mobile operating system or puts their device into standby.

Save the date and time you want it to appear somewhere. SharedPreferences would be one option, a backend API or the FireStore would be another.

When your app starts, read that date from the store and find out if it's time to show your banner. Alternatively, if you suspect your app is open for a long time and you want it to appear spontanously, set a timer to run this check periodically while your app is running.

Once you have shown the banner, add the next date to show it to the data store you picked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.