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();
}
CodePudding user response:
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..
CodePudding user response:
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
});
});
}
CodePudding user response:
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.