kamilgadek / to_do_list_kamilg

1 stars 0 forks source link

feat: LocalStorage uses sqflite to store data #41

Open mszakacz opened 1 month ago

mszakacz commented 1 month ago

Having our local_storage package, add sqflite to the package dependencies and use it for creating 2 methods: 1) readDoc(String docId) 2) readCollection(String collectionId) 3) setDoc(String collectionId, Map<String, dynamic> doc) - for document editing and creating new one 4) deleteDoc(String collectionId, String docId)

I do not know SQFLite so check docs and think how to write this methods. Maybe other strategy will be better for this package.

kamilgadek commented 1 month ago

I did some research and i think we can use https://pub.dev/packages/shared_preferences. Here is why it will be better than SQLite: Shared Preferences is a good option for storing small amounts of data in a Flutter app, especially for simple use cases like a to-do app. Here are some reasons why it might be better than SQLite for your specific needs:

Simplicity: Shared Preferences is easier to set up and use than SQLite. You don't need to create a database, define tables, or write SQL queries.

Lightweight: Shared Preferences is a lightweight solution that doesn't require a separate database engine. This can make it faster and more efficient for small amounts of data.

Key-value storage: Shared Preferences stores data as key-value pairs, which is a simple and intuitive way to store and retrieve data.

Persistence: Data stored in Shared Preferences persists even after the app is closed.

kamilgadek commented 1 month ago

@mszakacz

mszakacz commented 1 month ago

@kamilgadek For sure SharedPreferences in much easier to implement. However, few disadventages:

mszakacz commented 1 month ago

@kamilgadek Sorry, I just came across this sentence from SharePreferences docs: Data may be persisted to disk asynchronously, and there is no guarantee that writes will be persisted to disk after returning, so this plugin must not be used for storing critical data.

We don't want tasks in out app to disappear, do we? Let's use sqflite package instead

mszakacz commented 1 month ago

@kamilgadek I know it may be not so easy task for the beginning. On the other hand, most of the things we can take straight from documentation. Read the docs and think how we can achieve our goal. Let's start from sharing a semi-code here in this Issue.

I want you to write the following methods:

1) take all the tasks from data base 2) save new task in data base 3) edit existing task in data base 4) delete existing task form data base

Don't hesitate to reach me if any you have any questions

kamilgadek commented 1 month ago

@mszakacz Thanks for pointing me in the right direction! I've had a look at the sqflite docs and sketched out some methods to manage tasks in a local database. 1) getTasks(): Future<List<Map<String, dynamic>>> getTasks() async { final db = await database; return await db.query('tasks'); } 2)addTask(Map<String, dynamic> task): Future addTask(Map<String, dynamic> task) async { final db = await database; await db.insert( 'tasks', task, conflictAlgorithm: ConflictAlgorithm.replace, ); } 3)editTask(String taskId, Map<String, dynamic> updatedTask): Future editTask(String id, Map<String, dynamic> updatedTask) async { final db = await database; await db.update( 'tasks', updatedTask, where: 'id = ?', whereArgs: [id], ); } 4)deleteTask(String taskId): Future deleteTask(String id) async { final db = await database; await db.delete( 'tasks', where: 'id = ?', whereArgs: [id], ); }

mszakacz commented 1 month ago

@kamilgadek let's complete this Issue first as it's needed: https://github.com/kamilgadek/to_do_list_kamilg/issues/46