Search code examples
flutterstoragelocalsqflite

how to use propper update in sqflite flutter


i am new to flutter and i am trying to make a todo Task App using sqflite so everything be offline...

i made the add task and everything is working good but when it goes to the update like i wanna switch the favorite from 0 t 1 or vice versa i didnt found anthing on the net..

here is the code that i know its wrong :(

the sql code:

static Future<void> updateFavorite(
      String table, Map<String, Object> data) async {
    final db = await DBHelper.database();
    db.update(
      table,
      data,
      where: 'id = ?',
      whereArgs: data['id'],
    );
  }

here is my provider:

void switchFavorite({
    String id,
    String title,
    String description,
    String startDate,
    String finishDate,
    int repeated,
    int completed,
    int favorite,
  }) {
    final updatedTask = Task(
      id: id,
      title: title,
      description: description,
      startDate: startDate,
      finishDate: finishDate,
      completed: completed,
      favorite: favorite,
    );
    notifyListeners();
    print(updatedTask.id);
    DBHelper.updateFavorite('user_tasks', {
      'id': updatedTask.id,
      'title': updatedTask.title,
      'description': updatedTask.description,
      'startdate': updatedTask.startDate,
      'finishdate': updatedTask.finishDate,
      'completed': updatedTask.completed ?? 0,
      'favorite': updatedTask.favorite,
    });
  }

and here is how i want to use this by pressing icon button:

IconButton(
                            icon: tasks.tasks[i].favorite == 0
                                ? Icon(Icons.favorite_border)
                                : Icon(Icons.favorite),
                            onPressed: () {
                              Provider.of<TaskProvider>(
                                context,
                                listen: false,
                              ).switchFavorite(
                                id: tasks.tasks[i].id,
                                title: tasks.tasks[i].title,
                                description: tasks.tasks[i].description,
                                startDate: tasks.tasks[i].startDate,
                                finishDate: tasks.tasks[i].startDate,
                                favorite: tasks.tasks[i].favorite == 0 ? 1 : 0,
                              );
                            },
                          ),

hope you can help me please <3...


Solution

  • here it was my wrong usage:

    static Future<void> updateFavorite(
          String table, Map<String, Object> data) async {
        final db = await DBHelper.database();
        db.update(
          table,
          data,
          where: 'id = ?',
          whereArgs: data['id'],
        );
      }
    

    where i should use whereargs: [id] in this way