flutter FileSystemException (FileSystemException: 创建失败)

时间:2021-04-01 13:45:52

标签: flutter

  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))

enter image description here

1 个答案:

答案 0 :(得分:0)

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