我有几个目录需要创建,如果它们在应用程序的第一次运行时不存在。这些目录必须在应用程序的支持目录中创建。我不需要为此应用程序使用外部目录。所以,我确信我不需要在应用程序清单中为应用程序添加任何权限来访问外部存储目录。这是我在main()
函数中的代码:
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());
}
我正在使用最近升级到 Android 11(一个 UI 3)的三星移动设备。在开发我的应用程序时,我不得不卸载并重新安装它。每当我尝试重新安装它时,都会收到以下错误:
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):
我应该怎么做才能修复错误?谢谢