Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 946
Athira Reddy Avatar asked Aug 29 '26 12:08

Athira Reddy


1 Answers

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;
like image 112
Jitesh Mohite Avatar answered Aug 31 '26 08:08

Jitesh Mohite