1
votes

I wrote the below code that reads a tring from URL, writting the cotent into a file data.csv then trying to open the file to read its contents as csv but got an error that:

I/flutter ( 6145): FileSystemException: Cannot open file, path = 'data.csv' (OS Error: No such file or directory, errno = 2) I/flutter ( 6145): File is now closed. E/flutter ( 6145): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: FileSystemException: Cannot open file, path = 'data.csv' (OS Error: Read-only file system, errno = 30)

My code is:

import 'dart:convert';
import 'dart:io';
import 'dart:async';

  void _incrementCounter() {
    setState(() {
      new HttpClient().getUrl(Uri.parse('https://docs.google.com/spreadsheets/d/e/2PACX-1vQvf9tp4-fETDJbC-HRmRKvVFAXEAGO4lrYPpVeiYkB6nqqXdSs3CjX0eBMvjIoEeX9_qU6K2RWmzVk/pub?gid=0&single=true&output=csv'))
          .then((HttpClientRequest request) => request.close())
       //   .then((HttpClientResponse response) => response.transform(new Utf8Decoder()).listen(print));
       .then((HttpClientResponse response) => response.pipe(new File('data.csv').openWrite()));
      final File file = new File("data.csv");
      Stream<List> inputStream = file.openRead();
      inputStream
          .transform(utf8.decoder)       // Decode bytes to UTF-8.
          .transform(new LineSplitter()) // Convert stream to individual lines.
          .listen((String line) {        // Process results.

        List row = line.split(','); // split by comma

        String city = row[0];
        String branches = row[1];

        print('$city, $branches');

      },
          onDone: () { print('File is now closed.'); },
          onError: (e) { print(e.toString()); });

      // List<List<dynamic>> rowsAsListOfValues = const CsvToListConverter().convert(yourString);
      _counter++;
    });
  }
1

1 Answers

0
votes

Your code are not running in the order that you think so you will end up trying to read the file before it has been written. I think you code is going to be easier to understand if you use my answer for a other question you have asked: Reading data from url in List

And again, please read: https://dart.dev/codelabs/async-await