getApplicationDocumentsDirectory在Flutter桌面版(Windows)上无法正常工作

11

在Windows上的Flutter桌面应用程序中,如何保存和打开文件。在Flutter桌面上,getApplicationDocumentsDirectory无法工作。

Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  print('$directory');
  return directory.path;
}

抛出错误。

Exception has occurred
MissingPluginException (MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider))

你尝试过这个吗?https://flutter.dev/docs/cookbook/persistence/reading-writing-files - Alexandre Jean
@AlexJean 这在 Windows 应用程序上不起作用,就像在 Android 上一样,你可以自己尝试一下。 - Alok Kumar
这个链接对你有帮助吗?https://dev59.com/YLbna4cB1Zd3GeqPUg3S 我看到有人给你点了踩。我觉得有点不公平,特别是他们没有解释为什么这样做。 - Alexandre Jean
3个回答

4
在pubspec.yaml文件中添加以下内容:(path_provider: ^1.6.24) 在我们的文件中导入该包:(import 'package:path_provider/path_provider.dart';)

1
为了使桌面插件正常工作,我发现需要将Flutter专门为桌面提供的插件复制到我的项目中。我还更新了yaml配置。
插件源代码。 https://github.com/google/flutter-desktop-embedding/tree/master/plugins/flutter_plugins/path_provider_fde 注意:我相信这些插件将会改变,并最终成为默认安装的一部分。
像getApplicationDocumentsDirectory这样的内容是由Flutter提供的,但是Windows实现目前正在开发中,因此您需要提供具有Windows/Mac/Linux实现的其他插件,这些插件可以在Flutter桌面页面上找到。(上面的链接)
下面是我的文件夹的样子。

enter image description here

这是我的pubspec.yaml文件: dependencies: logger: ^0.9.1 dependencies: auto_size_text: ^2.1.0 draggable_scrollbar: ^0.0.4 provider: ^4.0.1 flutter: sdk: flutter file_chooser: git: url: git://github.com/google/flutter-desktop-embedding.git path: plugins/file_chooser ref: 4ee135c path_provider: ^1.5.1 path_provider_macos: ^0.0.1 path_provider_fde: path: ./plugins/flutter_plugins/path_provider_fde window_size: path: ./plugins/window_size url_launcher: ^5.4.0 url_launcher_fde: path: ./plugins/flutter_plugins/url_launcher_fde


-1
String _localPath = (await _findLocalPath()) + Platform.pathSeparator + 'Download';
final savedDir = Directory(_localPath);
bool hasExisted = await savedDir.exists();
if (!hasExisted) {
  savedDir.create();
}

Pubspec.yami

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^0.1.3
  path_provider: ^1.6.8 

亲,请尝试这段代码。


2
findLocalPath()未定义。哪个包含此函数? - Irfan Y

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接