-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from bmeaut/doc/chapter-5/update-materials
Doc/chapter 5/update materials
- Loading branch information
Showing
6 changed files
with
141 additions
and
58 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
projects/starters/chapter_05/lib/async/async_sample_1.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import "dart:async"; | ||
|
||
Future<String> myLongRunningFunction() => Future.delayed( | ||
Duration(seconds: 3), | ||
() { | ||
print("Inside the future"); | ||
throw Exception("No internet connection!"); | ||
return "Hello in the future!"; | ||
}, | ||
); | ||
|
||
void main() { | ||
print("Starting main"); | ||
var futureResult = myLongRunningFunction(); | ||
print("Immediate result: $futureResult"); | ||
futureResult | ||
.then( | ||
(result) { | ||
print("Result of function: $result"); | ||
}, | ||
) | ||
.timeout( | ||
Duration( | ||
seconds: 1, | ||
), | ||
) | ||
.catchError( | ||
(error) { | ||
print("Caught TimeoutException error: $error"); | ||
}, | ||
test: (e) => e is TimeoutException, | ||
) | ||
.catchError( | ||
(error) { | ||
print("Caught String error: $error"); | ||
}, | ||
test: (e) => e is String, | ||
) | ||
.whenComplete(() { | ||
print("Inside whenComplete"); | ||
}); | ||
var i = 0; | ||
/*while(true){ | ||
for (int j = 0; j < 1000; j++) | ||
print("${i++}"); | ||
} !!FREEZES!!*/ | ||
print("Ending main"); | ||
} |
42 changes: 42 additions & 0 deletions
42
projects/starters/chapter_05/lib/async/async_sample_2.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
Future<String> myLongRunningFunction() => Future.delayed( | ||
Duration(seconds: 3), | ||
() { | ||
print("Inside the future"); | ||
throw Exception("No internet connection!"); | ||
return "Hello in the future!"; | ||
}, | ||
); | ||
|
||
Future<String> myAsyncFunction() async { | ||
await Future.delayed(Duration(seconds: 3)); | ||
print("Inside the future"); | ||
throw Exception("No internet connection"); | ||
return "Hello in the future"; | ||
} | ||
|
||
void asyncVoidFunction() async { | ||
print("Starting async void function!"); | ||
await Future.delayed(Duration(seconds: 1)); | ||
print("Ending async void function!"); | ||
} | ||
|
||
void myAsyncTestFunction() async { | ||
print("Starting async func"); | ||
asyncVoidFunction(); | ||
//await asyncVoidFunction(); //!ERROR! Cannot await void function | ||
print("Calling long running function!"); | ||
try { | ||
var result = await myLongRunningFunction(); | ||
print("Immediate result: $result"); | ||
} catch (e) { | ||
print("Caught error: $e"); | ||
} finally { | ||
print("Inside finally"); | ||
} | ||
} | ||
|
||
void main() { | ||
print("Starting main function!"); | ||
myAsyncTestFunction(); | ||
print("Ending main function!"); | ||
} |
16 changes: 16 additions & 0 deletions
16
projects/starters/chapter_05/lib/async/generator_sample_1.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import "dart:math"; | ||
|
||
Iterable<int> calculatePrimes() sync* { | ||
yield 2; | ||
for (int i = 3; true; i++) { | ||
if (calculatePrimes() | ||
.takeWhile((prime) => prime <= sqrt(i)) | ||
.every((prime) => i % prime != 0)) { | ||
yield i; | ||
} | ||
} | ||
} | ||
|
||
void main() { | ||
calculatePrimes().take(20).forEach(print); | ||
} |
14 changes: 14 additions & 0 deletions
14
projects/starters/chapter_05/lib/async/generator_sample_2.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
Stream<int> myStreamGeneratorFunction() async* { | ||
yield 1; | ||
await Future.delayed(Duration(milliseconds: 200)); | ||
yield 2; | ||
await Future.delayed(Duration(milliseconds: 200)); | ||
yield 3; | ||
await Future.delayed(Duration(milliseconds: 200)); | ||
} | ||
|
||
void main() async { | ||
await for (var value in myStreamGeneratorFunction()) { | ||
print(value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.