【发布时间】:2022-11-17 18:35:20
【问题描述】:
我正在开发该应用程序,为此,我需要从存储中获取所有文件,它可以是 mp3、img pdf 或任何文件。我遵循了一个教程,他们在 flutter_file_manager 上工作。这个包已经停产了
【问题讨论】:
-
看看包裹
path_provider。
标签: flutter flutter-layout flutter-dependencies flutter-web flutter-test
我正在开发该应用程序,为此,我需要从存储中获取所有文件,它可以是 mp3、img pdf 或任何文件。我遵循了一个教程,他们在 flutter_file_manager 上工作。这个包已经停产了
【问题讨论】:
path_provider。
标签: flutter flutter-layout flutter-dependencies flutter-web flutter-test
您可以改用 file_picker 包。此外,您可能需要将 path_provider 包与 file_picker 包一起使用。可以在上述包的发布页面上找到完整的文档和示例。
以下是一些代码示例:
选择单个文件:
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
File file = File(result.files.single.path);
} else {
// User canceled the picker
}
选择多个文件:
FilePickerResult? result = await FilePicker.platform.pickFiles(allowMultiple: true);
if (result != null) {
List<File> files = result.paths.map((path) => File(path)).toList();
} else {
// User canceled the picker
}
要使用扩展过滤器选择多个文件:
FilePickerResult? result = await FilePicker.platform.pickFiles(
type: FileType.custom,
allowedExtensions: ['jpg', 'pdf', 'doc'],
);
选择一个目录:
String? selectedDirectory = await FilePicker.platform.getDirectoryPath();
if (selectedDirectory == null) {
// User canceled the picker
}
path_provider包的用法:
Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
【讨论】: