createDirectory method

Future<String> createDirectory({
  1. required BuildContext context,
  2. String? androidDirectoryPath,
})

To create the directory for saving the file context is required androidDirectoryPath is optional and default is Download directory path | if you want another android directory For iOS the directory is Application Documents Directory

Implementation

Future<String> createDirectory(
    {required BuildContext context, String? androidDirectoryPath}) async {
  String pathToDirectory = "";
  if (Platform.isAndroid) {
    //for android
    pathToDirectory =
        Directory(androidDirectoryPath ?? '/storage/emulated/0/Download')
            .path;
  } else if (Platform.isIOS) {
    //for iOS
    Directory directory = await getApplicationDocumentsDirectory();
    pathToDirectory = directory.path;
  } else {
    throw ScreenShotHelperException(
        cause:
            "Platform exception! (only Android & iOS Platforms compatible)");
  }

  bool isExists = await Directory(pathToDirectory).exists();

  if (!isExists) {
    Directory(pathToDirectory).create();
  }

  return pathToDirectory;
}