vandadnp / mynotes-course

This is the GitHub repository of the MyNotes application for the Free Flutter Course (https://www.youtube.com/playlist?list=PL6yRaaP0WPkVtoeNIGqILtRAgd3h2CNpT)
552 stars 198 forks source link

UpdateNotes method in step-15 updates all notes instead of only one note #3

Closed Muhab2001 closed 2 years ago

Muhab2001 commented 2 years ago

I found a problem in the following snippet of your amazing flutter course in step-15 at notes_service.dart

 Future<DatabaseNote> updateNote({
    required DatabaseNote note,
    required String text,
  }) async {
    await _ensureDbIsOpen();
    final db = _getDatabaseOrThrow();
    // make sure note exists
    await getNote(id: note.id);
    // >>>>>>>>>>>>>>>> The problem origin
    final updatesCount = await db.update(noteTable, {
      textColumn: text,
      isSyncedWithCloudColumn: 0,
    });
// >>>>>>>>>>>>>
    if (updatesCount == 0) {
      throw CouldNotUpdateNote();
    } else {
      final updatedNote = await getNote(id: note.id);
      _notes.removeWhere((note) => note.id == updatedNote.id);
      _notes.add(updatedNote);
      _notesStreamController.add(_notes);
      return updatedNote;
    }
  }

The result of this problem only prevails upon hot restart/reload of the app, as all the list elements will get the same value as the last created note due to that issue.

I fixed the problem in my codebase by specifying the exact id of the updated note, and the problem no longer persists

The fix

// update DB
    final updatesCount = await db.update(
      noteTable,
      {
        textColumn: text,
        isSyncedWithCloudColumn: 0,
      },
      where: 'id = ?',
      whereArgs: [note.id],
    );

Regards

vandadnp commented 2 years ago

Hi and thank you for creating this issue. This is something that will be fixed in the later chapters if you continue watching so please let me know if at the end of the course you're still seeing this issue.

Muhab2001 commented 2 years ago

I apologize for getting ahead of myself😅.

I did reach the fix in step-17.

More power to you my Vandad

vandadnp commented 2 years ago

Thank you for your engagement in the course. Keep going, great progress