Cannot create a directory in application folder using Flutter after upgrading to Android 11

764 views Asked by At

I have a couple of directories that need to be created if they do not exist on the app's first run. These directories have to be created within the application's support directories. I do not need to use external directories for this app. So, I am sure I do not need to add any permissions for the app in the application manifest for accessing external storage directories. Here is my code in the main() function:

Future<void> main() async {
  //This line is needed to use the main function as a Future
  WidgetsFlutterBinding.ensureInitialized();

  Directory directory = await getApplicationSupportDirectory();

  //Create downloads paths if they do not exist
  Directory literatureDirectory = Directory("${directory.path}/downloads/literature");
  Directory audioLecturesDirectory = Directory("${directory.path}/downloads/audioLectures");
  if (!(await literatureDirectory.exists())) {
    Directory literatureNewDirectory = await literatureDirectory.create();
  }
  if (!(await audioLecturesDirectory.exists())) {
    Directory audioLecturesNewDirectory = await audioLecturesDirectory.create();
  }

  runApp(ULTApp());
}

I am using a Samsung mobile device which was recently upgraded to Android 11 (One UI 3). While developing my app, I had to uninstall and re-install it. Whenever I try to re-install it, I get the following error:

E/flutter (22527): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: FileSystemException: Creation failed, path = '/data/user/0/com.example.app_name/files/downloads/literature' (OS Error: No such file or directory, errno = 2)
E/flutter (22527): #0      _Directory.create.<anonymous closure> (dart:io/directory_impl.dart:117:11)
E/flutter (22527): #1      _rootRunUnary (dart:async/zone.dart:1436:47)
E/flutter (22527): #2      _CustomZone.runUnary (dart:async/zone.dart:1335:19)
E/flutter (22527): <asynchronous suspension>
E/flutter (22527): #3      main (package:app_name/main.dart:24:40)
E/flutter (22527): <asynchronous suspension>
E/flutter (22527): 

What should I do to fix the error? Thanks

0

There are 0 answers