【问题标题】:flutter FileSystemException (FileSystemException: Creation failed)flutter FileSystemException(FileSystemException:创建失败)
【发布时间】:2021-04-01 13:45:52
【问题描述】:
  static Future<String> init() async {

    var installPath = "/Users/purplecity/code/likai_flutter/beiwanglu";
    Directory notePath = Directory("${installPath}/NoteStorage/");
    var noteDir = await notePath.exists();
    if (!noteDir) {
      await notePath.create();
    }

在 Flutter Device:ios 模拟器上没问题。但是在 Device:Android-x86 模拟器它有错误

我需要做什么?我不想使用资产.. 只是读取本地文件...

Error:

FileSystemException (FileSystemException: Creation failed, path = '/Users/purplecity/Library/Android/sdk/NoteStorage/' (OS Error: No such file or directory, errno = 2))

【问题讨论】:

  • 您在哪个 Android 版本上测试了该应用,而此 /Users/purplecity/code/likai_flutter/beiwanglu 不是有效的存储路径。

标签: flutter


【解决方案1】:

Flutter 允许您将资产(包括 json 等)放入您的应用程序。您手动执行此操作可能会导致此问题。

在您的 pubspec.yaml 中尝试以下操作

  flutter:
   assets:
     - NoteStorage/ 

确保标识完美无缺,pubspec.yaml 对此非常敏感。

然后你可以使用这个加载资产:

import 'dart:async' show Future;
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/NoteStorage/b1.json');
}

https://flutter.dev/docs/development/ui/assets-and-images

【讨论】:

  • 谢谢你!我尝试写一个笔记应用程序。我存储注释文本使用 json 格式文件。所以如果我添加一个新注释,那么需要创建一个新的 json 文件。它将在 NoteStorage/ 中创建?
  • 我认为这不适用于资产,因为它们是虚拟的。路径并不真正存在。在你的情况下,我会看看这个:flutter.dev/docs/cookbook/persistence/reading-writing-files
  • 如果“创建失败”再次发生,您可能需要更多 Android 权限。
猜你喜欢
  • 2022-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-26
  • 2021-07-09
相关资源
最近更新 更多