FileSystemException(FileSystemException:无法打开文件,PATH =''(OS错误:拒绝的权限,errno = 13))当flutter应用程序重新安装时
当我试图读取一个已经使用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'),
),
],
),
);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题可能与Android/iOS的权限系统有关。
检查此软件包,我认为它将解决您的问题。
cermission_handler
您需要添加类似的内容:
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: