【问题标题】:Get a list of files from the local storage and display it as a list in flutter web:从本地存储中获取文件列表并在 flutter web 中显示为列表:
【发布时间】:2022-11-18 12:16:53
【问题描述】:

我一直在构建一个 ios 应用程序,它会在其中从 API 下载一堆 pdf,并使用 flutter 中的 path_provider 包将其保存到本地存储:

这是我用来获取路径的代码:

Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
currentDir.value = directory.path;
return directory.path;} 

这就是我将其保存到设备的方式:

Future<void> saveFilesToLocal(String url, String fileName) async {
try {
  final path = await localPath;
  final file = File('$path/$fileName');
  if (await file.exists() == false) {
    final response = await Dio().get(url,
        options: Options(
            responseType: ResponseType.bytes,
            followRedirects: false,
            receiveTimeout: 0));
    final raf = file.openSync(mode: FileMode.write);
    raf.writeFromSync(response.data);
    await raf.close();
  } else {
    print("file already exists");
  }
} catch (e) {
  print('error: $e');
}

}

在我下载并保存文件到存储后,它会在主页中显示为一个gridview。要获取我使用的文件列表:

List<FileSystemEntity> files = [];


Future<void> getFilesList() async {
    files.clear();
    final path = await localPath;
    Directory dir = Directory('$path');
    files =
        dir.listSync(recursive: true, followLinks: false);
    update();
  }

所有这些在 ios 设备中都可以正常工作。但是我在尝试在网络上运行它时面临的当前问题是:

包 path_provider 不适用于 flutter web。我也被要求将其创建为网络应用程序,但我找不到替代方案。在寻找一个时,我看到一篇帖子说 flutter web 不允许访问设备的本地存储,所以这是不可能的。是真的吗?有解决方法吗?如果是关于安全性,该应用程序将只在特定设备上运行。它不会提供给公众。

【问题讨论】:

  • 这是真的。 Web 引擎在极其受限的环境中运行。谢天谢地。 :)

标签: flutter local-storage flutter-web


【解决方案1】:

谢谢,我在 flutter app 下载 pdf 文件时出现数据显示问题,显示已下载或未下载

【讨论】:

    猜你喜欢
    • 2023-01-19
    • 2018-02-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 2023-01-02
    • 1970-01-01
    • 2019-08-07
    • 1970-01-01
    相关资源
    最近更新 更多