backupDatabase method
Backs up the current SQLite database to a specified file path.
This method creates a copy of the existing database file at the given
backupPath. If a file already exists at backupPath, it will be
overwritten.
Usage:
String backupPath = '/path/to/backup/my_database_backup.db';
await backupDatabase(backupPath);
Throws an exception if the backup operation fails, such as due to permission issues or an invalid path.
Implementation
Future<File> backupDatabase(String backupPath) async {
// Retrieve the current database file path
var dbPath = path; // Implement this to return the path of your database
// Create a File object for the database file and the backup location
final databaseFile = File(dbPath);
final backupFile = File(backupPath);
// Check if the database file exists before proceeding
if (await databaseFile.exists()) {
// Create a backup by copying the database file
await databaseFile.copy(backupPath);
return backupFile;
} else {
throw Exception("Database file does not exist at $dbPath");
}
}