Skip to content

Instantly share code, notes, and snippets.

@Sameerkash
Created July 10, 2020 13:21
Show Gist options
  • Save Sameerkash/7305f9dc853ced34994527ade4a2e08e to your computer and use it in GitHub Desktop.
Save Sameerkash/7305f9dc853ced34994527ade4a2e08e to your computer and use it in GitHub Desktop.
// mark a todo as done
void toggle(Todo todo) {
   final currentState = state;
   if (currentState is TodoStateData) {
     final todos = currentState.todos.map((t) {
       if (t == todo) {
         var to = t.copyWith(
           isDone: !t.isDone,
         );
         read<LocalStorage>().updateTodo(to);
         return to;
       }
       return t;
     }).toList();

     state = TodoState(
       todos: todos,
     );
   }
 }

// Delete a todo
void delete(Todo todo) {
   final currentState = state;
   if (currentState is TodoStateData) {
     var list = currentState.todos.where((t) => t != todo).toList();
     if (list.isEmpty) {
       state = TodoState.empty();
     } else {
       state = TodoState(todos: list);
       read<LocalStorage>().deleteTodos(todo);
     }
    
   }
 }
}
 
 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment