Reading File in Dart

Issue

I am trying to read a file (made a simple .txt file on my desktop for that purpose) from machine using Dart on Dartium browser.

I have an input field, added a listener to it and here is how it looks:

  void _onFileSelect(event) {
    dom.File file = _fileInput.files.item(0);

    dom.FileReader reader = new dom.FileReader();
    //reader.readAsBinaryString(file);

    reader.addEventListener('load', (event) {
      print(reader.result); // This is the line I am having the problem with.
    });

    reader.readAsText(file);
  }

When I select the file, I get error:

Stack Trace: 0. Function: ‘Object.noSuchMethod’ url: ‘bootstrap’ line:717 col:3

on the line I marked using an inline-comment.

I have also set up a script to execute Dartium with the required flag:

#!/bin/sh
cd /opt/DartEditor/dart-sdk/chromium
./chrome.exe --allow-file-access-from-files $@

Can anyone give any tips where the problem could be?

P.S. I am using Ubuntu Precise x64.

Solution

Sorry, you can’t use the dart:io libraries in Dartium. Those libraries only work on Dart VM on the command line.

You might want to use an XMLHttpRequest to read in the file from an HTTP server.

Answered By – Seth Ladd

Answer Checked By – Marie Seifert (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *