Files
zapstore/lib/services/app_restart_service.dart
T

34 lines
1.1 KiB
Dart

import 'dart:io';
import 'package:flutter/services.dart';
import 'package:path_provider/path_provider.dart';
const _channel = MethodChannel('dev.zapstore/app_restart');
const _markerFileName = '.clear_on_restart';
/// Checks if storage should be cleared on this launch, and clears the marker.
/// Call this BEFORE initializing storage.
Future<void> maybeClearStorage(String dbPath) async {
final dir = await getApplicationSupportDirectory();
final marker = File('${dir.path}/$_markerFileName');
if (await marker.exists()) {
// Delete the main DB and SQLite WAL-mode companion files.
// Leaving stale -wal/-shm behind corrupts the seed database copy.
for (final suffix in ['', '-wal', '-shm']) {
final f = File('$dbPath$suffix');
if (await f.exists()) {
await f.delete();
}
}
await marker.delete();
}
}
/// Sets a marker file and triggers a native app restart.
Future<void> restartApp() async {
final dir = await getApplicationSupportDirectory();
final marker = File('${dir.path}/$_markerFileName');
await marker.create();
await _channel.invokeMethod('restart');
}