How can i solve "Multiple Sqflite Save" problem on Flutter

Viewed 62

I'm doing an e-commerce-like application. When the user likes a product, I save it with sqflite on another page called my favorites.But the problem is that the more the user presses the button, the more it saves. enter image description here

 GestureDetector(
            onTap: () async{
              Client rnd = Client(lastName: degerVer(widget.numUrunOzel));
              await DBProvider.db.newClient(rnd);
              setState(() {});
            },
            child: Icon(Icons.favorite))

First is my product page, Second is my favorites page.

1 Answers

Basically you need to check if object exists in db, if yes update, else add a new object

  1. using conflict algo

You can just insert all your data to the table setting the ConflictAlgorithm to replace or ignore

db.insert(table, data, conflictAlgorithm: ConflictAlgorithm.replace);

  1. Query, compare, replace/update

quite expensive to use.

db.query(table, columns: availableColumns, where: 'columnToQueryBy = ?', whereArgs: [queryParam]);

Related