Search code examples
androidsqliteflutterschedulersqflite

How to update table records in sqflite database on a specific date and time in a flutter app?(Android)


I've developed a flutter mobile application for Android. It has a 'Payment' data table. I need to update it on 31st January at 23.59 hours. How can I do this?

Invoking a function in the main class of the application is not suitable because if the app was not opened in that specific time I'm checking, the database will not be updated. So in my point of view this should be handled using an auto updating functionality.


Solution

  • I can think of two solutions for this:

    First: If the Payment table's data is only used in your app, then you don't need to update it on the same exact time. You can just write an if code that runs when app open (like in splash screen) to check if the desired time (31st January at 23.59 hours) is passed. If it is passed that time, then do the things you want to do.

    Second: You can use a workmanager. This is a plugin that runs your function in background. It has a registerOneOffTask function for your needs.

    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("1", "simpleTask"); //Android only (see below)
      runApp(MyApp());
    }