skip to Main Content

I am making a To-Do list application, and I’d like it on 24 hours to uncheck the tasks that have been checked.

Basically, alter the Checkbox widget’s check.

How, do I approach that?

I have tried using Workmanager, initialising a 24-hour background task when a task is checked.

But, if I use the callBackDispatcher method for the actions to take on 24 hours using work manager plugin, I cannot really update UI of the widgets, or access Lists / methods, etc, because they are in my widget, and callBackDispatcher must be outside.

More details – I store all my data in Firebase rtdb, also, I’d prefer to not use SharedPreferences.

I’d love if any of you could give me a way to approach this.

enter image description here

2

Answers


  1. You can follow a different approach without relying on WorkManager. Instead, you can use Firebase's Realtime Database and implement a mechanism to periodically update the task status.

    Note : Initialize Firebase Realtime Database in your application. You can refer to Firebase documentation on how to set it up.

    Refer : Firebase Realtime Database

    Steps :

    1. Create a data model for your tasks that includes properties such as task name, completion status, and a timestamp for when the task was last checked.
    class Task {
      String name;
      bool isCompleted;
      int lastCheckedTimestamp;
    
      Task({required this.name, this.isCompleted = false, required this.lastCheckedTimestamp});
    
      // Convert the object to a map for database storage
      Map<String, dynamic> toMap() {
        return {
          'name': name,
          'isCompleted': isCompleted,
          'lastCheckedTimestamp': lastCheckedTimestamp,
        };
      }
    
      // Create a Task object from a map retrieved from the database
      factory Task.fromMap(Map<String, dynamic> map) {
        return Task(
          name: map['name'],
          isCompleted: map['isCompleted'],
          lastCheckedTimestamp: map['lastCheckedTimestamp'],
        );
      }
    }
    
    
    1. Whenever a task is checked or unchecked, update the corresponding task in the Firebase Realtime Database.
    void updateTaskCompletionStatus(Task task) {
      task.isCompleted = !task.isCompleted;
      task.lastCheckedTimestamp = DateTime.now().millisecondsSinceEpoch;
      
      // Update the task in the Firebase Realtime Database
      FirebaseDatabase.instance.reference().child('tasks').child(taskId).update(task.toMap());
    }
    
    
    1. Set up a periodic timer using the Timer.periodic function. This timer will run a callback every 24 hours to update the task status.
    void startPeriodicTaskStatusUpdate() {
      Timer.periodic(const Duration(hours: 24), (_) {
        // Query the Firebase Realtime Database to retrieve all tasks
        FirebaseDatabase.instance.reference().child('tasks').once().then((DataSnapshot snapshot) {
          final tasks = Map<String, Task>.from(snapshot.value ?? {});
          
          // Iterate over each task and update the completion status if necessary
          tasks.forEach((taskId, taskData) {
            final task = Task.fromMap(taskData);
            
            // If the task was checked more than 24 hours ago and is completed, uncheck it
            final currentTime = DateTime.now().millisecondsSinceEpoch;
            if (task.isCompleted && (currentTime - task.lastCheckedTimestamp) > 24 * 60 * 60 * 1000) {
              task.isCompleted = false;
              
              // Update the task in the Firebase Realtime Database
              FirebaseDatabase.instance.reference().child('tasks').child(taskId).update(task.toMap());
            }
          });
        });
      });
    }
    
    
    1. Call the startPeriodicTaskStatusUpdate function when your application starts or when the user logs in.
    Login or Signup to reply.
  2. if I were you I would create a control table that lists all the affected Checkbox object, checked flag and last checked date. Reset the checked flag using stored procedure that runs every hour to see whether the 24 hour period has lapsed.
    SQLite or plain CSV should suffice

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search