Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement Flutter clear user app data and storage on logout

Tags:

flutter

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..

like image 700
Mdev Avatar asked Jun 24 '20 04:06

Mdev


People also ask

How do I clear app data Flutter?

Open android studio Tools->Flutter->Clean. Go to File -> Invalidate Caches / Restart.

How do I clear cached image in Flutter?

Go to Tools > Flutter > Flutter Clean to clear the build cache of the Flutter project.

How do you execute a function after a period of inactivity in Flutter?

Periodic timer is used to execute callback repeatedly (e.g., every 10 seconds); Timer sets when the widget initializes and when any tap happens. Any following tap will cancel the old timer and create a new one.


2 Answers

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

like image 68
iamdipanshus Avatar answered Nov 15 '22 03:11

iamdipanshus


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);
      }
    }
like image 25
Alireza aslami Avatar answered Nov 15 '22 03:11

Alireza aslami