如何让一段代码每天只运行一次?

Pra*_* BB 1 android ios flutter

我正在使用颤振。如何每天运行一次函数?我无法使用循环或异步函数每 24 小时运行一次。有没有办法可以实现这一点?

我想在用户打开应用程序时在启动屏幕中运行它。

小智 5

我试过了,看看是否有效

void main() async {
      //checking current date
      final currentdate = new DateTime.now().day;
      //you need to import this Shared preferences plugin 
      SharedPreferences prefs = await SharedPreferences.getInstance();
      //getting last date 
      int lastDay = (prefs.getInt('day') ?? 0);
      
      //check is code already display or not
      if(currentdate!=lastDay){
         await prefs.setInt('day', currentdate);
        //your code will run once in day
        print("hello will display once")
      }
    
    }
Run Code Online (Sandbox Code Playgroud)