From 6c754fa27c88cbe8b2deb0473dcd60dab6cd6f5c Mon Sep 17 00:00:00 2001 From: Csongor Vogel Date: Sat, 9 Mar 2024 16:52:20 +0200 Subject: [PATCH 1/3] Add the snippets to the project --- .../chapter_05/lib/async/async_sample_1.dart | 48 +++++++++++++++++++ .../chapter_05/lib/async/async_sample_2.dart | 42 ++++++++++++++++ .../lib/async/generator_sample_1.dart | 16 +++++++ .../lib/async/generator_sample_2.dart | 14 ++++++ 4 files changed, 120 insertions(+) create mode 100644 projects/starters/chapter_05/lib/async/async_sample_1.dart create mode 100644 projects/starters/chapter_05/lib/async/async_sample_2.dart create mode 100644 projects/starters/chapter_05/lib/async/generator_sample_1.dart create mode 100644 projects/starters/chapter_05/lib/async/generator_sample_2.dart diff --git a/projects/starters/chapter_05/lib/async/async_sample_1.dart b/projects/starters/chapter_05/lib/async/async_sample_1.dart new file mode 100644 index 00000000..533f4943 --- /dev/null +++ b/projects/starters/chapter_05/lib/async/async_sample_1.dart @@ -0,0 +1,48 @@ +import "dart:async"; + +Future 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"); +} diff --git a/projects/starters/chapter_05/lib/async/async_sample_2.dart b/projects/starters/chapter_05/lib/async/async_sample_2.dart new file mode 100644 index 00000000..d9c80a03 --- /dev/null +++ b/projects/starters/chapter_05/lib/async/async_sample_2.dart @@ -0,0 +1,42 @@ +Future myLongRunningFunction() => Future.delayed( + Duration(seconds: 3), + () { + print("Inside the future"); + throw Exception("No internet connection!"); + return "Hello in the future!"; + }, + ); + +Future 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!"); +} diff --git a/projects/starters/chapter_05/lib/async/generator_sample_1.dart b/projects/starters/chapter_05/lib/async/generator_sample_1.dart new file mode 100644 index 00000000..543f091e --- /dev/null +++ b/projects/starters/chapter_05/lib/async/generator_sample_1.dart @@ -0,0 +1,16 @@ +import "dart:math"; + +Iterable 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); +} diff --git a/projects/starters/chapter_05/lib/async/generator_sample_2.dart b/projects/starters/chapter_05/lib/async/generator_sample_2.dart new file mode 100644 index 00000000..98896ee7 --- /dev/null +++ b/projects/starters/chapter_05/lib/async/generator_sample_2.dart @@ -0,0 +1,14 @@ +Stream 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); + } +} From f7c20ea5c47176c703318d38170b76150087c36c Mon Sep 17 00:00:00 2001 From: Csongor Vogel Date: Sat, 9 Mar 2024 16:53:20 +0200 Subject: [PATCH 2/3] Remove unnecessary widget_test.dart --- .../starters/chapter_05/test/widget_test.dart | 30 ------------------- 1 file changed, 30 deletions(-) delete mode 100644 projects/starters/chapter_05/test/widget_test.dart diff --git a/projects/starters/chapter_05/test/widget_test.dart b/projects/starters/chapter_05/test/widget_test.dart deleted file mode 100644 index e17089ff..00000000 --- a/projects/starters/chapter_05/test/widget_test.dart +++ /dev/null @@ -1,30 +0,0 @@ -// This is a basic Flutter widget test. -// -// To perform an interaction with a widget in your test, use the WidgetTester -// utility that Flutter provides. For example, you can send tap and scroll -// gestures. You can also use WidgetTester to find child widgets in the widget -// tree, read text, and verify that the values of widget properties are correct. - -import 'package:flutter/material.dart'; -import 'package:flutter_test/flutter_test.dart'; - -import 'package:flutter_counter_provider/main.dart'; - -void main() { - testWidgets('Counter increments smoke test', (WidgetTester tester) async { - // Build our app and trigger a frame. - await tester.pumpWidget(MyApp()); - - // Verify that our counter starts at 0. - expect(find.text('0'), findsOneWidget); - expect(find.text('1'), findsNothing); - - // Tap the '+' icon and trigger a frame. - await tester.tap(find.byIcon(Icons.add)); - await tester.pump(); - - // Verify that our counter has incremented. - expect(find.text('0'), findsNothing); - expect(find.text('1'), findsOneWidget); - }); -} From e076814d9630048aa32bd40aa1e01552c05c4e08 Mon Sep 17 00:00:00 2001 From: Csongor Vogel Date: Sat, 9 Mar 2024 16:54:07 +0200 Subject: [PATCH 3/3] Update dependencies --- projects/starters/chapter_05/pubspec.lock | 49 ++++++++++------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/projects/starters/chapter_05/pubspec.lock b/projects/starters/chapter_05/pubspec.lock index 8077d238..e15edce1 100644 --- a/projects/starters/chapter_05/pubspec.lock +++ b/projects/starters/chapter_05/pubspec.lock @@ -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: @@ -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 @@ -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 @@ -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: @@ -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"