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

Commit f72851d

Browse files
committed
Merge pull request #93 from damondouglas/master
Fix #92: rewrite samples using async await.
2 parents 23cf11e + 11631ee commit f72851d

File tree

56 files changed

+429
-409
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+429
-409
lines changed

dart_io_mini_samples/example/files_directories_and_symlinks/deleting_a_file_directory_or_symlink.dart

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,19 @@
55
/// Use the FilesSystemEntity `delete()` method to delete a file, directory, or
66
/// symlink. This method is inherited by File, Directory, and Link.
77
8-
98
import 'dart:io';
109

11-
void main() {
10+
main() async {
11+
1212
// Create a temporary directory.
13-
Directory.systemTemp.createTemp('my_temp_dir')
14-
.then((directory) {
15-
// Confirm it exists.
16-
directory.exists().then(print); // Prints 'true'.
17-
// Delete the directory.
18-
return directory.delete();
19-
})
20-
.then((directory) {
21-
// Confirm it no longer exists.
22-
directory.exists().then(print); // Prints 'false'
23-
});
13+
var dir = await Directory.systemTemp.createTemp('my_temp_dir');
14+
15+
// Confirm it exists.
16+
print(await dir.exists());
17+
18+
// Delete the directory.
19+
await dir.delete();
20+
21+
// Confirm it no longer exists.
22+
print(await dir.exists());
2423
}

dart_io_mini_samples/example/files_directories_and_symlinks/directories/creating_a_directory.dart

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,8 @@
88
99
import 'dart:io';
1010

11-
void main() {
11+
main() async {
1212
// Creates dir/ and dir/subdir/.
13-
new Directory('dir/subdir').create(recursive: true)
14-
// The created directory is returned as a Future.
15-
.then((Directory directory) {
16-
print(directory.path);
17-
});
18-
}
13+
var directory = await new Directory('dir/subdir').create(recursive: true);
14+
print(directory.path);
15+
}

dart_io_mini_samples/example/files_directories_and_symlinks/directories/creating_a_temporary_directory.dart

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,7 @@
88
99
import 'dart:io';
1010

11-
void main() {
12-
// Create a temporary directory in the system temp directory.
13-
Directory.systemTemp.createTemp('my_temp_dir')
14-
.then((directory) {
15-
print(directory.path);
16-
});
11+
main() async {
12+
var directory = await Directory.systemTemp.createTemp('my_temp_dir');
13+
print(directory.path);
1714
}

dart_io_mini_samples/example/files_directories_and_symlinks/directories/listing_the_contents_of_a_directory.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
/// `false` (default is `true`).
99
1010
import 'dart:io';
11+
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.
1112

12-
void main() {
13+
main() async {
1314
// Get the system temp directory.
1415
var systemTempDir = Directory.systemTemp;
1516

1617
// List directory contents, recursing into sub-directories, but not following
1718
// symbolic links.
18-
systemTempDir.list(recursive: true, followLinks: false)
19-
.listen((FileSystemEntity entity) {
20-
print(entity.path);
21-
});
19+
Stream<FileSystemEntity> entityList =
20+
systemTempDir.list(recursive: true, followLinks: false);
21+
await for (FileSystemEntity entity in entityList) print(entity.path);
2222
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/creating_a_file.dart

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,13 @@
88
99
import 'dart:io';
1010

11-
void main() {
11+
main() async {
1212

1313
// Get the system temp directory.
1414
var systemTempDir = Directory.systemTemp;
15-
1615
// Creates dir/, dir/subdir/, and dir/subdir/file.txt in the system
1716
// temp directory.
18-
new File('${systemTempDir.path}/dir/subdir/file.txt').create(recursive: true)
19-
// The created file is returned as a Future.
20-
.then((file) {
21-
print(file.path);
22-
});
23-
}
17+
var file = await new File('${systemTempDir.path}/dir/subdir/file.txt').create(
18+
recursive: true);
19+
print(file.path);
20+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/handling_errors_when_reading_a_file.dart

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,15 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
/// Use `then()` to read the file contents, and `catchError()` to catch
6-
/// errors. Register a callback with `catchError()` to handle the error.
7-
85
import 'dart:io';
96

10-
void handleError(e) {
11-
print('There was a ${e.runtimeType} error');
12-
print(e.message);
13-
}
14-
15-
main() {
7+
main() async {
168
final filename = 'non_existent_file.txt';
17-
new File(filename).readAsString()
18-
// Read and print the file contents.
19-
.then(print)
20-
// Catch errors.
21-
.catchError((e) {
22-
print('There was a ${e.runtimeType} error');
23-
print('Could not read $filename');
24-
});
25-
}
9+
try {
10+
var file = await new File(filename).readAsString();
11+
print(file);
12+
} catch (e) {
13+
print('There was a ${e.runtimeType} error');
14+
print('Could not read $filename');
15+
}
16+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/reading_a_file_as_a_string.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
77
import 'dart:io';
88

9-
void main() {
10-
new File('file.txt').readAsString().then((String contents) {
11-
// Do something with the file contents.
12-
});
13-
}
9+
main() async {
10+
var contents = await new File('file.txt').readAsString();
11+
print(contents);
12+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/reading_a_file_as_bytes.dart

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ import 'dart:io';
88

99
import 'package:crypto/crypto.dart';
1010

11-
void main() {
12-
new File('file.txt').readAsBytes().then((bytes) {
13-
// Do something with the bytes. For example, convert to base64.
14-
String base64 = CryptoUtils.bytesToBase64(bytes);
15-
// ...
16-
});
17-
}
11+
main() async {
12+
var bytes = await new File('file.txt').readAsBytes();
13+
// Do something with the bytes. For example, convert to base64.
14+
String base64 = CryptoUtils.bytesToBase64(bytes);
15+
print(base64);
16+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/reading_a_file_as_lines.dart

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,7 @@
66
77
import 'dart:io';
88

9-
void main() {
10-
new File('file.txt').readAsLines().then((List<String> lines) {
11-
// Do something with lines.
12-
});
13-
}
9+
main() async {
10+
List<String> lines = await new File('file.txt').readAsLines();
11+
lines.forEach((String line) => print(line));
12+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/reading_a_file_using_a_stream.dart

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,21 @@ import 'dart:io';
1313
import 'dart:convert';
1414
import 'dart:async';
1515

16-
main() {
16+
main() async {
1717
final file = new File('file.txt');
1818
Stream<List<int>> inputStream = file.openRead();
1919

20-
inputStream
21-
// Decode to UTF8.
22-
.transform(UTF8.decoder)
23-
// Convert stream to individual lines.
24-
.transform(new LineSplitter())
25-
// Process results.
26-
.listen((String line) {
27-
print('$line: ${line.length} bytes');
28-
},
29-
onDone: () { print('File is now closed.'); },
30-
onError: (e) { print(e.toString()); });
31-
}
20+
Stream<String> lines = inputStream
21+
// Decode to UTF8.
22+
.transform(UTF8.decoder)
23+
// Convert stream to individual lines.
24+
.transform(new LineSplitter());
25+
26+
try {
27+
await for (String line in lines) print('$line: ${line.length} bytes');
28+
} catch (e) {
29+
print(e.toString());
30+
}
31+
32+
print('File is now closed.');
33+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/writing_a_string_to_a_file.dart

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,8 @@
77
88
import 'dart:io';
99

10-
void main() {
10+
main() async {
1111
final filename = 'file.txt';
12-
new File(filename).writeAsString('some content')
13-
.then((File file) {
14-
// Do something with the file.
15-
});
16-
}
12+
var file = await new File(filename).writeAsString('some content');
13+
print("Content written to $file");
14+
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/writing_bytes_to_a_file.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,15 @@
77
import 'dart:io';
88
import 'dart:convert';
99

10-
void main() {
10+
main() async {
1111
final string = 'Dart!';
12+
1213
// Encode to UTF8.
1314
var encodedData = UTF8.encode(string);
15+
var file = await new File('file.txt');
16+
file.writeAsBytes(encodedData);
17+
var data = await file.readAsBytes();
1418

15-
new File('file.txt')
16-
.writeAsBytes(encodedData)
17-
.then((file) => file.readAsBytes())
18-
.then((data) {
19-
// Decode to a string, and print.
20-
print(UTF8.decode(data)); // Prints 'Dart!'.
21-
});
19+
// Decode to a string, and print.
20+
print(UTF8.decode(data)); // Prints 'Dart!'.
2221
}

dart_io_mini_samples/example/files_directories_and_symlinks/files/writing_to_a_file_using_a_stream.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
1010
import 'dart:io';
1111

12-
void main() {
12+
main() {
1313
var file = new File('file.txt');
1414
var sink = file.openWrite();
1515
sink.write('FILE ACCESSED ${new DateTime.now()}\n');
1616

1717
// Close the IOSink to free system resources.
1818
sink.close();
19-
}
19+
}

dart_io_mini_samples/example/files_directories_and_symlinks/finding_the_type_of_a_filesystem_object.dart

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,32 @@
66
/// object. This method is inherited by File, Directory, and Link.
77
88
import 'dart:io';
9+
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.
910

10-
void main() {
11+
main() async {
1112
// List the contents of the system temp directory.
12-
Directory.systemTemp.list(recursive: true, followLinks: false)
13-
.listen((FileSystemEntity entity) {
14-
// Get the type of the FileSystemEntity, apply the appropiate label, and
15-
// print the entity path.
16-
FileSystemEntity.type(entity.path)
17-
.then((FileSystemEntityType type) {
18-
String label;
19-
switch (type) {
20-
case FileSystemEntityType.DIRECTORY:
21-
label = 'D';
22-
break;
23-
case FileSystemEntityType.FILE:
24-
label = 'F';
25-
break;
26-
case FileSystemEntityType.LINK:
27-
label = 'L';
28-
break;
29-
default:
30-
label = 'UNKNOWN';
31-
}
32-
print('$label: ${entity.path}');
33-
});
34-
});
35-
}
13+
Stream<FileSystemEntity> entityList =
14+
Directory.systemTemp.list(recursive: true, followLinks: false);
15+
16+
await for (FileSystemEntity entity in entityList) {
17+
// Get the type of the FileSystemEntity, apply the appropiate label, and
18+
// print the entity path.
19+
FileSystemEntityType type = await FileSystemEntity.type(entity.path);
20+
21+
String label;
22+
switch (type) {
23+
case FileSystemEntityType.DIRECTORY:
24+
label = 'D';
25+
break;
26+
case FileSystemEntityType.FILE:
27+
label = 'F';
28+
break;
29+
case FileSystemEntityType.LINK:
30+
label = 'L';
31+
break;
32+
default:
33+
label = 'UNKNOWN';
34+
}
35+
print('$label: ${entity.path}');
36+
}
37+
}

dart_io_mini_samples/example/files_directories_and_symlinks/getting_the_parent_directory.dart

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
/// and Link.
88
99
import 'dart:io';
10+
import 'dart:async'; // Import not needed but added here to explicitly assign type for clarity below.
1011

11-
void main() {
12+
main() async {
1213
// List the contents of the system temp directory.
13-
Directory.systemTemp.list(recursive: true, followLinks: false)
14-
.listen((FileSystemEntity entity) {
15-
// Print the path of the parent of each file, directory, and symlink.
16-
print(entity.parent.path);
17-
});
18-
}
14+
Stream<FileSystemEntity> entityList =
15+
Directory.systemTemp.list(recursive: true, followLinks: false);
16+
17+
await for (FileSystemEntity entity in entityList) print(entity.parent.path);
18+
}

dart_io_mini_samples/example/files_directories_and_symlinks/renaming_a_file_directory_or_symlink.dart

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@
88
99
import 'dart:io';
1010

11-
void main() {
11+
main() async {
1212
// Get the system temp directory.
1313
var systemTempDir = Directory.systemTemp;
1414

1515
// Create a file.
16-
new File('${systemTempDir.path}/foo.txt').create()
17-
.then((file) {
18-
print('The path is ${file.path}'); // Prints path ending with `foo.txt`.
19-
// Rename the file.
20-
return file.rename('${systemTempDir.path}/bar.txt');
21-
})
22-
.then((file) {
23-
print('The path is ${file.path}'); // Prints path ending with `bar.txt`.
24-
});
25-
}
16+
var file = await new File('${systemTempDir.path}/foo.txt').create();
17+
18+
// Prints path ending with `foo.txt`.
19+
print('The path is ${file.path}');
20+
21+
// Rename the file.
22+
await file.rename('${systemTempDir.path}/bar.txt');
23+
24+
// Prints path ending with `bar.txt`.
25+
print('The path is ${file.path}');
26+
}

0 commit comments

Comments
 (0)