Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Doc/chapter 5/update materials #73

Merged
merged 3 commits into from
Mar 18, 2024
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
48 changes: 48 additions & 0 deletions projects/starters/chapter_05/lib/async/async_sample_1.dart
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 projects/starters/chapter_05/lib/async/async_sample_2.dart
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 projects/starters/chapter_05/lib/async/generator_sample_1.dart
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 projects/starters/chapter_05/lib/async/generator_sample_2.dart
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);
}
}
49 changes: 21 additions & 28 deletions projects/starters/chapter_05/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ packages:
name: async
url: "https://pub.dartlang.org"
source: hosted
version: "2.8.1"
version: "2.9.0"
boolean_selector:
dependency: transitive
description:
Expand All @@ -21,35 +21,28 @@ packages:
name: characters
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
charcode:
dependency: transitive
description:
name: charcode
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
version: "1.2.1"
clock:
dependency: transitive
description:
name: clock
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
collection:
dependency: transitive
description:
name: collection
url: "https://pub.dartlang.org"
source: hosted
version: "1.15.0"
version: "1.16.0"
fake_async:
dependency: transitive
description:
name: fake_async
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.3.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -66,21 +59,28 @@ packages:
name: matcher
url: "https://pub.dartlang.org"
source: hosted
version: "0.12.10"
version: "0.12.12"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.5"
meta:
dependency: transitive
description:
name: meta
url: "https://pub.dartlang.org"
source: hosted
version: "1.7.0"
version: "1.8.0"
path:
dependency: transitive
description:
name: path
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.0"
version: "1.8.2"
sky_engine:
dependency: transitive
description: flutter
Expand All @@ -92,7 +92,7 @@ packages:
name: source_span
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.1"
version: "1.9.0"
stack_trace:
dependency: transitive
description:
Expand All @@ -113,34 +113,27 @@ packages:
name: string_scanner
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.0"
version: "1.1.1"
term_glyph:
dependency: transitive
description:
name: term_glyph
url: "https://pub.dartlang.org"
source: hosted
version: "1.2.0"
version: "1.2.1"
test_api:
dependency: transitive
description:
name: test_api
url: "https://pub.dartlang.org"
source: hosted
version: "0.4.2"
typed_data:
dependency: transitive
description:
name: typed_data
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.0"
version: "0.4.12"
vector_math:
dependency: transitive
description:
name: vector_math
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.0"
version: "2.1.2"
sdks:
dart: ">=2.12.0 <3.0.0"
dart: ">=2.17.0-0 <3.0.0"
30 changes: 0 additions & 30 deletions projects/starters/chapter_05/test/widget_test.dart

This file was deleted.