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