writeDatabase method

  1. @override
Future<void> writeDatabase(
  1. ByteData data
)
override

Implementation

@override
Future<void> writeDatabase(ByteData data) async {
  final List<int> bytes =
      data.buffer.asUint8List(data.offsetInBytes, data.lengthInBytes);
  try {
    final databaseFactory = databaseFactoryFfi;
    sqfliteFfiInit();
    final path = join(
        getFinalDatabasePath(await databaseFactory.getDatabasesPath()),
        connection!.databaseName);
    if (File(path).existsSync()) {
      await databaseFactory.deleteDatabase(path);
      if (File('$path-wal').existsSync()) {
        File('$path-wal').deleteSync();
      }
    }
    File(path).writeAsBytesSync(bytes, mode: FileMode.write);
    print('The database has been written to $path successfully');
  } catch (e) {
    throw Exception(e.toString());
  }
}