Book Image

DART Cookbook

By : Ivo Balbaert
Book Image

DART Cookbook

By: Ivo Balbaert

Overview of this book

Table of Contents (18 chapters)
Dart Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Reading and processing a file line by line


Files containing data in the comma separated values (csv) format are structured so that one line contains data about one object, so we need a way to read and process the file line by line. As an example, we use the data file winequality-red.csv, that contains 1,599 sample measurements, 12 data columns, such as pH and alcohol, per sample, separated by a semicolon (;), of which you can see the top 20 in the following screenshot:

How to do it...

Examine the code of the command-line project processing_lines using the following methods:

  1. Using the readAsLines method as shown in the following code:

    import 'dart:io';
    // for step 3:
    import 'dart:async';
    import 'dart:convert';
    
    main() {
      File data= new File("../winequality-red.csv");
      data.readAsLines().then(processLines)
      .catchError((e) => handleError(e));
    }
    
    processLines(List<String> lines) {
      // process lines:
      for (var line in lines) {
      print(line);
      }
    }
    
    handleError(e) {
      print("An error...