Flutter:如何使用 path_provider 将文件写入本地目录?
Flutter: how do I write a file to local directory with path_provider?
我正在开发 Flutter 应用程序,需要将文件写入本地系统(iOS、Android 以及 Web)。我在逻辑上遵循了 Read and Write Files, and used 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!');
}
这个测试函数只是通过一个按钮调用,没有什么疯狂的。
但是当我 运行 在浏览器中跳动并按下按钮时,我收到以下错误:
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
似乎这个问题是众所周知的,已遵循 and here 的建议,包括:
- 运行ning
flutter clean
并重新获取包
- 完全关闭应用程序并重新运行宁
- 正在将 Flutter 升级到最新的稳定版
- 重新添加
path_provider
到我的 pubspec.yaml
我正在使用最新版本的依赖项 (path_provider: ^2.0.9
)。
非常感谢任何帮助。
下面是评论中提到的data 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'));
确保它位于仅在 html
可用时导入的 dart 文件中,或者最好使用 package:universal_html
.
我正在开发 Flutter 应用程序,需要将文件写入本地系统(iOS、Android 以及 Web)。我在逻辑上遵循了 Read and Write Files, and used 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!');
}
这个测试函数只是通过一个按钮调用,没有什么疯狂的。 但是当我 运行 在浏览器中跳动并按下按钮时,我收到以下错误:
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
似乎这个问题是众所周知的,已遵循
- 运行ning
flutter clean
并重新获取包 - 完全关闭应用程序并重新运行宁
- 正在将 Flutter 升级到最新的稳定版
- 重新添加
path_provider
到我的 pubspec.yaml
我正在使用最新版本的依赖项 (path_provider: ^2.0.9
)。
非常感谢任何帮助。
下面是评论中提到的data 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'));
确保它位于仅在 html
可用时导入的 dart 文件中,或者最好使用 package:universal_html
.