简体   繁体   English

Flutter:如何使用 path_provider 将文件写入本地目录?

[英]Flutter: how do I write a file to local directory with path_provider?

I'm working on a Flutter app and need to write files to the local system (iOS, Android but also Web).我正在开发一个 Flutter 应用程序,需要将文件写入本地系统(iOS、Android 以及 Web)。 I have logically followed the documentation on Read and Write Files , and used path_provider to make a small example to test functionality:我在逻辑上遵循了Read and Write Files上的文档,并使用path_provider做了一个小例子来测试功能:

import 'package:path_provider/path_provider.dart';

...

  void _testPathProvider() async {
      // Directory appDocDir = await getTemporaryDirectory(); // Using a different dir. not working either
      // String appDocPath = appDocDir.path;

      Directory appDocDir = await getApplicationDocumentsDirectory();
      String appDocPath = appDocDir.path;
      debugPrint('$appDocPath/my_file.txt');

      // Save in local file system
      var file = await File('$appDocPath/my_file.txt').writeAsString('hello!');
  }

This test function is simply called by a button, nothing crazy there.这个测试 function 只是通过一个按钮调用,没有什么疯狂的。 But when I run flutter in browser & press button, I get following error:但是当我在浏览器中运行 flutter 并按下按钮时,出现以下错误:

Error: MissingPluginException(No implementation found for method getApplicationDocumentsDirectory on channel plugins.flutter.io/path_provider)
    at Object.throw_ [as throw] (http://localhost:42561/dart_sdk.js:5067:11)
at MethodChannel._invokeMethod (http://localhost:42561/packages/flutter/src/services/restoration.dart.lib.js:1560:21)
    at _invokeMethod.next (<anonymous>)
    at http://localhost:42561/dart_sdk.js:40571:33
    at _RootZone.runUnary (http://localhost:42561/dart_sdk.js:40441:59)
    at _FutureListener.thenAwait.handleValue (http://localhost:42561/dart_sdk.js:35363:29)
    at handleValueCallback (http://localhost:42561/dart_sdk.js:35931:49)
    at Function._propagateToListeners (http://localhost:42561/dart_sdk.js:35969:17)
    at _Future.new.[_completeWithValue] (http://localhost:42561/dart_sdk.js:35817:23)
    at async._AsyncCallbackEntry.new.callback (http://localhost:42561/dart_sdk.js:35838:35)
    at Object._microtaskLoop (http://localhost:42561/dart_sdk.js:40708:13)
    at _startMicrotaskLoop (http://localhost:42561/dart_sdk.js:40714:13)
    at http://localhost:42561/dart_sdk.js:36191:9

Seems like the issue is well-know, have followed advice both here and here , including:似乎这个问题是众所周知的,已遵循此处此处的建议,包括:

  • running flutter clean and re-getting packages运行flutter clean并重新获取包
  • closing app completely and re-running完全关闭应用程序并重新运行
  • upgrading Flutter to latest stable将 Flutter 升级到最新的稳定版
  • re-adding path_provider to my pubspec.yamlpath_provider重新添加到我的 pubspec.yaml

I'm using latest version of the dependency ( path_provider: ^2.0.9 ).我正在使用最新版本的依赖项( path_provider: ^2.0.9 )。

Any help much appreciated.非常感谢任何帮助。

Here's an example of the data uri method mentioned in the comments:这是评论中提到的数据uri方法的示例:

import 'dart:html' as html;

  final bytes = utf8.encode('hello!');
  final dataUri = 'data:text/plain;base64,${base64.encode(bytes)}';
  html.document.createElement('a') as html.AnchorElement
    ..href = dataUri
    ..download = 'my_file.txt'
    ..dispatchEvent(html.Event.eventType('MouseEvent', 'click'));

Make sure that's in a dart file that's only imported when html is available, or preferably, use package:universal_html .确保它位于 dart 文件中,该文件仅在html可用时导入,或者最好使用package:universal_html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Flutter 如何使用path_provider获取本地下载文件夹 - Flutter how get local download folder with path_provider 如何使用path_provider包在flutter中构建登录页面以将登录信息存储在本地文件中 - How to build a login page in flutter using path_provider package for storing login in local file 如何更新 Flutter 中的现有文件(Path_Provider) - How to update the Existing File (Path_Provider) in Flutter Flutter path_provider 问题 - Flutter path_provider issue Flutter path_provider 插件不起作用 - (操作系统错误:没有这样的文件或目录,errno = 2) - Flutter path_provider plugin not working - (OS Error: No such file or directory, errno = 2) Flutter andoid 模拟器在哪里使用path_provider将文件保存到Windows本地磁盘? - Where does Flutter andoid emulator save file to Windows local disk with path_provider? Flutter 使用 path_provider 获取外部存储目录和 MediaStore package - Flutter get External Storage Directory and MediaStore with path_provider package 如何在flutter中显示来自path_provider的视频? - How to display video from path_provider in flutter? Flutter的&#39;path_provider&#39;依赖项中的未定义类 - Undefined class in 'path_provider' dependency for Flutter Flutter path_provider 和 sdk 版本不匹配 - Flutter path_provider and sdk version mismatch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM