Read and write files and call web APIs in a Dart CLI

10 steps · about 3 h 45 min · Beginner

  1. Build your first app (tutorial, dart.dev, 10 min)

    Start by creating and running a Dart CLI project, so every later example has a place to live.

  2. Asynchronous programming: futures, async, await (interactive, dart.dev, 30 min)

    File and network calls in Dart return Futures, so learn async and await before touching either.

  3. dart:io (doc, dart.dev, 20 min)

    This is the official tour of reading text, writing with an IOSink and streaming files in command-line code.

  4. File class - dart:io library - Dart API (reference, Dart API docs, 15 min)

    It shows the whole File API, including the async and sync method pairs and when to stream large files.

  5. writeAsString method - File class - dart:io library - Dart API (reference, Dart API docs, 5 min)

    This page covers the details of overwriting versus appending with FileMode, and flushing, which cause the usual write bugs.

  6. Write command-line apps (tutorial, dart.dev, 30 min)

    Build the dcat sample to practise reading files, parsing arguments with package:args and printing to stdout.

  7. Fetch data from the internet (tutorial, dart.dev, 30 min)

    Move from files to the network: build a Uri, call http.get, then decode the JSON into a class.

  8. http (reference, pub.dev, 15 min)

    Read the package's own page for Client usage, RetryClient and how the client differs across platforms.

  9. Work with JSON data (tutorial, dart.dev, 30 min)

    Learn to validate decoded JSON safely with pattern matching and fromJson constructors.

  10. Fetch data from the internet (tutorial, dart.dev, 40 min)

    Put it all together by calling the Wikipedia API from a CLI and handling responses and query parameters.