Skip to content
This repository has been archived by the owner on Jun 3, 2020. It is now read-only.

Fix #92: rewrite samples using async await. #93

Merged
merged 3 commits into from
Oct 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,19 @@
/// Use the FilesSystemEntity `delete()` method to delete a file, directory, or
/// symlink. This method is inherited by File, Directory, and Link.


import 'dart:io';

void main() {
main() async {

// Create a temporary directory.
Directory.systemTemp.createTemp('my_temp_dir')
.then((directory) {
// Confirm it exists.
directory.exists().then(print); // Prints 'true'.
// Delete the directory.
return directory.delete();
})
.then((directory) {
// Confirm it no longer exists.
directory.exists().then(print); // Prints 'false'
});
var dir = await Directory.systemTemp.createTemp('my_temp_dir');

// Confirm it exists.
print(await dir.exists());

// Delete the directory.
await dir.delete();

// Confirm it no longer exists.
print(await dir.exists());
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

import 'dart:io';

void main() {
main() async {
// Creates dir/ and dir/subdir/.
new Directory('dir/subdir').create(recursive: true)
// The created directory is returned as a Future.
.then((Directory directory) {
print(directory.path);
});
}
var directory = await new Directory('dir/subdir').create(recursive: true);
print(directory.path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@

import 'dart:io';

void main() {
// Create a temporary directory in the system temp directory.
Directory.systemTemp.createTemp('my_temp_dir')
.then((directory) {
print(directory.path);
});
main() async {
var directory = await Directory.systemTemp.createTemp('my_temp_dir');
print(directory.path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
/// `false` (default is `true`).

import 'dart:io';
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.

void main() {
main() async {
// Get the system temp directory.
var systemTempDir = Directory.systemTemp;

// List directory contents, recursing into sub-directories, but not following
// symbolic links.
systemTempDir.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) {
print(entity.path);
});
Stream<FileSystemEntity> entityList =
systemTempDir.list(recursive: true, followLinks: false);
await for (FileSystemEntity entity in entityList) print(entity.path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,13 @@

import 'dart:io';

void main() {
main() async {

// Get the system temp directory.
var systemTempDir = Directory.systemTemp;

// Creates dir/, dir/subdir/, and dir/subdir/file.txt in the system
// temp directory.
new File('${systemTempDir.path}/dir/subdir/file.txt').create(recursive: true)
// The created file is returned as a Future.
.then((file) {
print(file.path);
});
}
var file = await new File('${systemTempDir.path}/dir/subdir/file.txt').create(
recursive: true);
print(file.path);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.

/// Use `then()` to read the file contents, and `catchError()` to catch
/// errors. Register a callback with `catchError()` to handle the error.

import 'dart:io';

void handleError(e) {
print('There was a ${e.runtimeType} error');
print(e.message);
}

main() {
main() async {
final filename = 'non_existent_file.txt';
new File(filename).readAsString()
// Read and print the file contents.
.then(print)
// Catch errors.
.catchError((e) {
print('There was a ${e.runtimeType} error');
print('Could not read $filename');
});
}
try {
var file = await new File(filename).readAsString();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add print(file) to match original code?

print(file);
} catch (e) {
print('There was a ${e.runtimeType} error');
print('Could not read $filename');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import 'dart:io';

void main() {
new File('file.txt').readAsString().then((String contents) {
// Do something with the file contents.
});
}
main() async {
var contents = await new File('file.txt').readAsString();
print(contents);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import 'dart:io';

import 'package:crypto/crypto.dart';

void main() {
new File('file.txt').readAsBytes().then((bytes) {
// Do something with the bytes. For example, convert to base64.
String base64 = CryptoUtils.bytesToBase64(bytes);
// ...
});
}
main() async {
var bytes = await new File('file.txt').readAsBytes();
// Do something with the bytes. For example, convert to base64.
String base64 = CryptoUtils.bytesToBase64(bytes);
print(base64);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@

import 'dart:io';

void main() {
new File('file.txt').readAsLines().then((List<String> lines) {
// Do something with lines.
});
}
main() async {
List<String> lines = await new File('file.txt').readAsLines();
lines.forEach((String line) => print(line));
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,21 @@ import 'dart:io';
import 'dart:convert';
import 'dart:async';

main() {
main() async {
final file = new File('file.txt');
Stream<List<int>> inputStream = file.openRead();

inputStream
// Decode to UTF8.
.transform(UTF8.decoder)
// Convert stream to individual lines.
.transform(new LineSplitter())
// Process results.
.listen((String line) {
print('$line: ${line.length} bytes');
},
onDone: () { print('File is now closed.'); },
onError: (e) { print(e.toString()); });
}
Stream<String> lines = inputStream
// Decode to UTF8.
.transform(UTF8.decoder)
// Convert stream to individual lines.
.transform(new LineSplitter());

try {
await for (String line in lines) print('$line: ${line.length} bytes');
} catch (e) {
print(e.toString());
}

print('File is now closed.');
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,8 @@

import 'dart:io';

void main() {
main() async {
final filename = 'file.txt';
new File(filename).writeAsString('some content')
.then((File file) {
// Do something with the file.
});
}
var file = await new File(filename).writeAsString('some content');
print("Content written to $file");
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@
import 'dart:io';
import 'dart:convert';

void main() {
main() async {
final string = 'Dart!';

// Encode to UTF8.
var encodedData = UTF8.encode(string);
var file = await new File('file.txt');
file.writeAsBytes(encodedData);
var data = await file.readAsBytes();

new File('file.txt')
.writeAsBytes(encodedData)
.then((file) => file.readAsBytes())
.then((data) {
// Decode to a string, and print.
print(UTF8.decode(data)); // Prints 'Dart!'.
});
// Decode to a string, and print.
print(UTF8.decode(data)); // Prints 'Dart!'.
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

import 'dart:io';

void main() {
main() {
var file = new File('file.txt');
var sink = file.openWrite();
sink.write('FILE ACCESSED ${new DateTime.now()}\n');

// Close the IOSink to free system resources.
sink.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,32 @@
/// object. This method is inherited by File, Directory, and Link.

import 'dart:io';
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.

void main() {
main() async {
// List the contents of the system temp directory.
Directory.systemTemp.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) {
// Get the type of the FileSystemEntity, apply the appropiate label, and
// print the entity path.
FileSystemEntity.type(entity.path)
.then((FileSystemEntityType type) {
String label;
switch (type) {
case FileSystemEntityType.DIRECTORY:
label = 'D';
break;
case FileSystemEntityType.FILE:
label = 'F';
break;
case FileSystemEntityType.LINK:
label = 'L';
break;
default:
label = 'UNKNOWN';
}
print('$label: ${entity.path}');
});
});
}
Stream<FileSystemEntity> entityList =
Directory.systemTemp.list(recursive: true, followLinks: false);

await for (FileSystemEntity entity in entityList) {
// Get the type of the FileSystemEntity, apply the appropiate label, and
// print the entity path.
FileSystemEntityType type = await FileSystemEntity.type(entity.path);

String label;
switch (type) {
case FileSystemEntityType.DIRECTORY:
label = 'D';
break;
case FileSystemEntityType.FILE:
label = 'F';
break;
case FileSystemEntityType.LINK:
label = 'L';
break;
default:
label = 'UNKNOWN';
}
print('$label: ${entity.path}');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
/// and Link.

import 'dart:io';
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.

void main() {
main() async {
// List the contents of the system temp directory.
Directory.systemTemp.list(recursive: true, followLinks: false)
.listen((FileSystemEntity entity) {
// Print the path of the parent of each file, directory, and symlink.
print(entity.parent.path);
});
}
Stream<FileSystemEntity> entityList =
Directory.systemTemp.list(recursive: true, followLinks: false);

await for (FileSystemEntity entity in entityList) print(entity.parent.path);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious about whether the formatter would try to break this into multiple lines...

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,19 @@

import 'dart:io';

void main() {
main() async {
// Get the system temp directory.
var systemTempDir = Directory.systemTemp;

// Create a file.
new File('${systemTempDir.path}/foo.txt').create()
.then((file) {
print('The path is ${file.path}'); // Prints path ending with `foo.txt`.
// Rename the file.
return file.rename('${systemTempDir.path}/bar.txt');
})
.then((file) {
print('The path is ${file.path}'); // Prints path ending with `bar.txt`.
});
}
var file = await new File('${systemTempDir.path}/foo.txt').create();

// Prints path ending with `foo.txt`.
print('The path is ${file.path}');

// Rename the file.
await file.rename('${systemTempDir.path}/bar.txt');

// Prints path ending with `bar.txt`.
print('The path is ${file.path}');
}
Loading