flutter how to empty a directory code example

Example 1: flutter delete file

Future<String> get _localPath async {
    final directory = await getApplicationDocumentsDirectory();

    return directory.path;
  }

  Future<File> get _localFile async {
    final path = await _localPath;
    print('path ${path}');
    return File('$path/counter.txt');
  }

  Future<int> deleteFile() async {
        try {
          final file = await _localFile;

          await file.delete();
        } catch (e) {
          return 0;
        }
      }

Example 2: flutter delete directory

import 'dart:io';

void main() {
    final dir = Directory(dirPath);
    dir.deleteSync(recursive: true);
}

Tags:

Dart Example