Reputation: 377
I would like to clear all my app data and the cache when the app's starting or closing. My purpose is to start the app as the first time every time I open it. In my main.dart
file I'm using this code with the plugin path_provider:
Future<void> _deleteCacheDir() async {
final cacheDir = await getTemporaryDirectory();
if (cacheDir.existsSync()) {
cacheDir.deleteSync(recursive: true);
}
}
Future<void> _deleteAppDir() async {
final appDir = await getApplicationSupportDirectory();
if (appDir.existsSync()) {
appDir.deleteSync(recursive: true);
}
}
void main() async {
_deleteCacheDir(); // To clear cache
_deleteAppDir(); // To clear app's storage
WidgetsFlutterBinding.ensureInitialized(); // Initialize plugins
await Firebase.initializeApp(); // Initialize Firebase
var initializationSettingsAndroid =
AndroidInitializationSettings('circle_ic_appliguide');
var initializationSettingsIOS = IOSInitializationSettings(
requestAlertPermission: true,
requestBadgePermission: true,
requestSoundPermission: true,
onDidReceiveLocalNotification:
(int? id, String? title, String? body, String? payload) async {});
var initializationSettings = InitializationSettings(
android: initializationSettingsAndroid, iOS: initializationSettingsIOS);
await flutterLocalNotificationsPlugin.initialize(initializationSettings,
onSelectNotification: (String? payload) async {
if (payload != null) {
debugPrint('notification payload: ' + payload);
}
});
runApp(MyApp()); // Run the App
}
But it seems do not work... Am I using the good method to release my purpose?
Upvotes: 0
Views: 3720
Reputation: 1
Get the temp folder and delete files in it. Consider a below code snippet:
var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);
Upvotes: 3