Dart cheatsheet
Published 3 months ago: October 25, 2020
Dart cheatsheet.
Index
This cheatsheet is a small collection of handy snippets for quick reference in tricky situations. Feel free to suggest edits, propose additions, complain about something, disagree with content or such and the like. Every feedback is a welcome one.
Useful libraries
Standard library
- dart:async - Enables async/await functions
- dart:io - For read/write operations on the local filesystem
- dart:convert - Used for, among other things, JSON encoding and decoding
print('Hello, World!');
Basic function
void main() { print('Hello, World!'); }
Run Dart script from the command line
dart /path/to/script.dart
Directory
var myDir = new Directory('/home/user/dir/');
List files in a directory
var myDir = new Directory('/home/user/dir/');
void checkDir(theDir) {
theDir.exists().then((isThere) {
isThere ? print('exists') : print('non-existent');
listDir(theDir);
});
}
void listDir(theDir) {
theDir.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) {
print(entity.path);
});
}
checkDir(myDir);