FileSystemException(FileSystemException:无法打开文件,PATH =''(OS错误:拒绝的权限,errno = 13))当flutter应用程序重新安装时

发布于 2025-02-10 08:55:22 字数 2161 浏览 1 评论 0原文

当我试图读取一个已经使用flutter应用程序创建的外部文件时,我的行为很奇怪。读取和写作工作正常,但是当我卸载该应用程序并再次运行时,它会给我Filesystemexception。 我创建了一个非常简单的一切来演示这个问题。 我真的被困住了,没有解决方案!

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _message = '';
  String _content = '';

  void _writeFile() {
    setState(() {
      File file = File('/storage/emulated/0/Documents/test2.txt');
      if (file.existsSync()) {
        _message = 'File already exsists';
        _content = '';
      } else {
        String fileContent = '{"key","value"}';
        file.writeAsString(fileContent);
        _message = 'File created';
        _content = fileContent;
      }
    });
  }

  Future<void> _readFile() async {
    {
      File file = File('/storage/emulated/0/Documents/test2.txt');
      if (!file.existsSync()) {
        _message = 'File is not there!';
      } else {
        _message = 'File read :';
        _content = await file.readAsString();
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_message),
            const Text(''),
            Text(_content),
          ],
        ),
      ),
      floatingActionButton: Row(
        children: [
          const SizedBox(width: 100),
          FloatingActionButton(
            onPressed: _writeFile,
            tooltip: 'Write',
            child: const Text('Write'),
          ),
          const SizedBox(width: 100),
          FloatingActionButton(
            onPressed: _readFile,
            tooltip: 'Read',
            child: const Text('Read'),
          ),
        ],
      ),
    );
  }
}

在此处输入图像描述

I am having a weird behavior when I am trying to read a an external file that already created with a flutter app when I re-install the app. Reading and writing work just fine but when I uninstall the app and run it again it gives me FileSystemException.
I created a very simple all to demonstration the issue.
I am really stuck and ran out of solutions!

class MyHomePage extends StatefulWidget {
  const MyHomePage({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  String _message = '';
  String _content = '';

  void _writeFile() {
    setState(() {
      File file = File('/storage/emulated/0/Documents/test2.txt');
      if (file.existsSync()) {
        _message = 'File already exsists';
        _content = '';
      } else {
        String fileContent = '{"key","value"}';
        file.writeAsString(fileContent);
        _message = 'File created';
        _content = fileContent;
      }
    });
  }

  Future<void> _readFile() async {
    {
      File file = File('/storage/emulated/0/Documents/test2.txt');
      if (!file.existsSync()) {
        _message = 'File is not there!';
      } else {
        _message = 'File read :';
        _content = await file.readAsString();
      }
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(_message),
            const Text(''),
            Text(_content),
          ],
        ),
      ),
      floatingActionButton: Row(
        children: [
          const SizedBox(width: 100),
          FloatingActionButton(
            onPressed: _writeFile,
            tooltip: 'Write',
            child: const Text('Write'),
          ),
          const SizedBox(width: 100),
          FloatingActionButton(
            onPressed: _readFile,
            tooltip: 'Read',
            child: const Text('Read'),
          ),
        ],
      ),
    );
  }
}

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月棠 2025-02-17 08:55:22

这个问题可能与Android/iOS的权限系统有关。
检查此软件包,我认为它将解决您的问题。

cermission_handler

您需要添加类似的内容:

var status = await Permission.storage.status;
if (status.isDenied) {
  Map<Permission, PermissionStatus> statuses = await [
    Permission.location,
    Permission.storage,
  ].request();
} else {
  // Your method calls here
}

Probably this issue is related to permissions system of Android/iOS.
Check this package, I think it's gonna solve your problem.

permission_handler

You need to add something like this:

var status = await Permission.storage.status;
if (status.isDenied) {
  Map<Permission, PermissionStatus> statuses = await [
    Permission.location,
    Permission.storage,
  ].request();
} else {
  // Your method calls here
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文