3

I am new to Flutter development. I just started to make Note app with Provider pattern, but when I update note, It won't update and It just create a new note in list. But when I minimize and return back list is updated. when I click on the item, it shows old data. Help me

sqlite_helper.dart

import 'package:sqflite/sqflite.dart' as sql;
import 'package:path/path.dart' as path;
import 'package:sqflite/sqlite_api.dart';

class DBHelper {
  static Future<Database> database() async {
    final dbPath = await sql.getDatabasesPath();
    return sql.openDatabase(path.join(dbPath, 'mydatabase.db'),
        onCreate: (db, version) {
      return db.execute(
          'CREATE TABLE notes(id TEXT PRIMARY KEY, title TEXT, content TEXT)');
    }, version: 1);
  }

  static Future<void> insert(String table, Map<String, Object> data) async {
    final db = await DBHelper.database();
    db.insert(
      table,
      data,
      conflictAlgorithm: ConflictAlgorithm.replace,
    );
  }
}

Athira Reddy
  • 1,004
  • 14
  • 18

1 Answers1

2

NoteProvider doesn't have update logic, here _items.add(newNote) will add an item at the end. So the existing list item not going to get updated.

Below will be an example of how to list items can be updated, you can still around this there are a lot of answers available.

_items[_items.indexWhere((note) => note.id == newNote.id)] = newNote;
Jitesh Mohite
  • 31,138
  • 12
  • 157
  • 147
  • ya. that works, conflictAlgorithm: ConflictAlgorithm.replace, then why this not replacing the note? – Athira Reddy Sep 24 '20 at 11:25
  • That is for database not for Provider class, Provider locals variables should be updated as well. Could you upvote and accept the answer? – Jitesh Mohite Sep 24 '20 at 11:27