Flutter, path_provider and application states : Cleaning cached file in temporary directory , does it need special authorisations?

147 Views Asked by At

i'm using the flutter package path_provider to cache some json files in a temporary directory. I want these file to be deleted when i'm closing the app using this part of code :

@override
  void didChangeAppLifecycleState(AppLifecycleState state){
    if (state == AppLifecycleState.paused && !cleaningInProgress) {
      // Transitioning from paused to inactive, initiate cache cleaning
      cleaningInProgress = true;
    } else if (state == AppLifecycleState.inactive && cleaningInProgress) {
      // App is now inactive, perform cache cleaning
      _deleteCachedJsonfiles();
      cleaningInProgress = false;
    }
  }

Here's the code for _deleteCachedJsonfiles() :

Future<void> _deleteCachedJsonfiles() async {
    try {
      Directory tempDir = await getTemporaryDirectory();
      for (var file in tempDir.listSync()) {
        if (file.path.endsWith('.json')) {
          file.delete();
        }
      }
    } catch (e) {
      print('Error while cleaning cache: $e');
    }
    cleaningInProgress = false;
  }

On the integrated VM of intelliji JetBrains i see that the json files are effectively deleted but it does'nt seems the same when i do it on a real phone. Does i need a special authorisation in AndroidManifest.xml like : <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

0

There are 0 best solutions below