How to fix "FileSystemException: Cannot open file" in flutter when reading and writing files?
Asked Answered
D

2

10

I'm pretty new to dart/flutter, so let me know if there is anything I can clarify. I'm trying to download a file, but when I try to access it, I get the error

FileSystemException: Cannot open file, path = './data.csv' (OS Error: Read-only file system, errno = 30)

What am I doing wrong, and how can I fix this?

 downloadTextFile() {
    HttpClient client = new HttpClient();
    client.getUrl(Uri.parse('I put the link I used here, but it is sort of long'))
        .then((HttpClientRequest request) {
      return request.close();
    })
        .then((HttpClientResponse response) {
      response.pipe(new File('./data.csv').openWrite());
    });
    readFileByLines();
  }

  void readFileByLines() {
    File file = new File('./data.csv');
    List<String> lines = file.readAsLinesSync();
    lines.forEach((l) => print(l));
  }
Dowser answered 19/3, 2020 at 1:45 Comment(0)
L
8

You can use package https://pub.dev/packages/path_provider
And write your file to tempPath or appDocPath

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path;

Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

For Android , In AndroidManifest.xml , you can add permission

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Lighterage answered 19/3, 2020 at 2:16 Comment(0)
P
0

you can use open_file_plus without any permission issue.

Perdurable answered 27/3 at 21:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.