Read and write files and call web APIs in a Dart CLI
10 steps · about 3 h 45 min · Beginner
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- 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.
- http (reference, pub.dev, 15 min)
Read the package's own page for Client usage, RetryClient and how the client differs across platforms.
- Work with JSON data (tutorial, dart.dev, 30 min)
Learn to validate decoded JSON safely with pattern matching and fromJson constructors.
- 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.