import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import 'package:todo/database/DatabaseInterface.dart'; import '../buisness/ToDoItem.dart'; class Sqlite implements DatabaseInterface { static final Sqlite _instance = Sqlite._internal(); factory Sqlite() => _instance; static Database? _database; Sqlite._internal(); Future get database async { if (_database != null) return _database!; _database = await _initDatabase(); return _database!; } Future _initDatabase() async { String path = join(await getDatabasesPath(), 'todo_database.db'); print(path); return await openDatabase( path, version: 1, onCreate: (db, version) async { await db.execute( ''' CREATE TABLE todo_items( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, description TEXT, dueDate TEXT, status TEXT ) ''', ); }, ); } // ToDoItem hinzufügen @override Future insertToDoItem(ToDoItem item) async { final db = await database; final id = await db.insert( 'todo_items', item.toJson(), conflictAlgorithm: ConflictAlgorithm.replace, ); item.id = id; } @override Future> getToDoItems() async { final db = await database; final List> maps = await db.query('todo_items'); return List.generate(maps.length, (i) { return ToDoItem.fromJson(maps[i]); }); } @override Future deleteToDoItem(int id) async { final db = await database; await db.delete( 'todo_items', where: 'id = ?', whereArgs: [id], ); } @override Future updateToDoItem(ToDoItem item) async { final db = await database; await db.update( 'todo_items', item.toJson(), where: 'id = ?', whereArgs: [item.id], ); } Future closeDatabase() async { final db = await database; await db.close(); } }