Home > Blockchain >  How to run on a schedule by flutter
How to run on a schedule by flutter

Time:09-12

I want to create timer app on flutter, I can't know how to schedul of run. If you know how to do that,Would like to tell me. This sentence was transrated,so I'm sorry if mistaken.

CodePudding user response:

You can try this:

void main() {
  scheduleTimeout(5 * 1000); // 5 seconds.
}

Timer scheduleTimeout([int milliseconds = 10000]) =>
    Timer(Duration(milliseconds: milliseconds), handleTimeout);

void handleTimeout() {  // callback function
  // Do some work.
}

This function waits 5 seconds before it calls the callback handleTimeout.

Refer to this: https://api.flutter.dev/flutter/dart-async/Timer-class.html

CodePudding user response:

You might try to use the work manager plugin. workmanager. Even when the app is closed, it will execute task even when app is closed. For ex:

void callbackDispatcher() {
  Workmanager().executeTask((task, inputData) {
    print("Native called background task: $backgroundTask"); //simpleTask will be emitted here.
    return Future.value(true);
  });
}

void main() {
  Workmanager().initialize(
    callbackDispatcher, // The top level function, aka callbackDispatcher
    isInDebugMode: true // If enabled it will post a notification whenever the task is running. Handy for debugging tasks
  );
  Workmanager().registerOneOffTask("task-identifier", "simpleTask");
  runApp(MyApp());
}

CodePudding user response:

You can use Timer function to achieve schedule

  • Related