Athira Reddy
Athira Reddy

Reputation: 1054

Flutter ListView won't Update

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,
    );
  }
}

Upvotes: 3

Views: 227

Answers (1)

Jitesh Mohite
Jitesh Mohite

Reputation: 34210

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;

Upvotes: 2

Related Questions