The following line you've provided is trying to clear the device's temporary folder.
var appDir = (await getTemporaryDirectory()).path;
new Directory(appDir).delete(recursive: true);
But sometimes avoiding to clear the temp folder will do better and clearing the app's cache folder instead. Just like this:
var appDir = (await getTemporaryDirectory()).path + '/<package_name>';
new Directory(appDir).delete(recursive: true);
Where package_name
refers to the app's Bundle ID.
There are also some packages that you could use like the path_provider package as mentioned in the comments section. It allows us to access commonly used locations on the device’s filesystem. You can do something like this:
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);
}
}
Here is a good reference for path_provider.