import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart'; class GymSet { int? id; String name; int reps; int weight; DateTime created; GymSet( {this.id, required this.name, required this.reps, required this.weight, required this.created}); } class GymSetDatabaseHelper { static const _databaseName = "gym_set_database.db"; static const _databaseVersion = 1; static const table = 'gym_sets'; static const columnId = '_id'; static const columnName = 'name'; static const columnReps = 'reps'; static const columnWeight = 'weight'; static const columnCreated = 'created'; GymSetDatabaseHelper._privateConstructor(); static final GymSetDatabaseHelper instance = GymSetDatabaseHelper._privateConstructor(); static Database? _database; Future get database async { if (_database != null) return _database!; _database = await _initDatabase(); return _database!; } _initDatabase() async { String path = join(await getDatabasesPath(), _databaseName); return await openDatabase(path, version: _databaseVersion, onCreate: _onCreate); } Future _onCreate(Database db, int version) async { await db.execute(''' CREATE TABLE $table ( $columnId INTEGER PRIMARY KEY, $columnName TEXT NOT NULL, $columnReps INTEGER NOT NULL, $columnWeight INTEGER NOT NULL, $columnCreated TEXT NOT NULL ) '''); } Future insert(GymSet gymSet) async { Database db = await instance.database; return await db.insert(table, { columnName: gymSet.name, columnReps: gymSet.reps, columnWeight: gymSet.weight, columnCreated: gymSet.created.toIso8601String() }); } Future> getAll() async { Database db = await instance.database; List> result = await db.query(table); return result .map((gymSetMap) => GymSet( id: gymSetMap[columnId], name: gymSetMap[columnName], reps: gymSetMap[columnReps], weight: gymSetMap[columnWeight], created: DateTime.parse(gymSetMap[columnCreated]), )) .toList(); } Future update(GymSet gymSet) async { Database db = await instance.database; return await db.update( table, { columnName: gymSet.name, columnReps: gymSet.reps, columnWeight: gymSet.weight, columnCreated: gymSet.created.toIso8601String() }, where: '$columnId = ?', whereArgs: [gymSet.id]); } Future delete(GymSet gymSet) async { Database db = await instance.database; return await db .delete(table, where: '$columnId = ?', whereArgs: [gymSet.id]); } }