8

How to implement Flutter App clear user app data and storage on logout.. i want to clear everything (shared preferences, global variables, singletons, local storage, cache) this should be equivalent to Android > settings > App > clear storage & clear cache..

Mdev
  • 81
  • 1
  • 2

2 Answers2

4

first install path_provider

/// this will delete cache
Future<void> _deleteCacheDir() async {
    final cacheDir = await getTemporaryDirectory();

    if (cacheDir.existsSync()) {
      cacheDir.deleteSync(recursive: true);
    }
}

/// this will delete app's storage
Future<void> _deleteAppDir() async {
    final appDir = await getApplicationSupportDirectory();

    if(appDir.existsSync()){
      appDir.deleteSync(recursive: true);
    }
}

see the original answer here

iamdipanshus
  • 492
  • 3
  • 12
1

you must use from path_provider to get your app TemporaryDirectory and app Directory on the filesystem.

    import 'dart:io';
    import 'package:path_provider/path_provider.dart';
    
    Future<void> _deleteCacheDir() async {
    Directory tempDir = await getTemporaryDirectory();
    
      if (tempDir.existsSync()) {
        tempDir.deleteSync(recursive: true);
      }
    }
    
    Future<void> _deleteAppDir() async {
     Directory appDocDir = await getApplicationDocumentsDirectory();

      if (appDocDir.existsSync()) {
        appDocDir.deleteSync(recursive: true);
      }
    }
xamantra
  • 930
  • 7
  • 10
Aslami.dev
  • 880
  • 8
  • 19