11import 'package:df_safer_dart/df_safer_dart.dart' ;
22import 'dart:convert' ;
33
4+ void main () async {
5+ for (var id in [1 , 2 , 3 , 4 , 5 ]) {
6+ print ('Processing User ID: $id ' );
7+
8+ // Execute the pipeline. `await value` opens the Async box.
9+ final finalResult = await getUserNotificationSound (id).value;
10+
11+ switch (finalResult) {
12+ case Ok (value: final optionSound):
13+ switch (optionSound) {
14+ // Success! The value is an Option<String>.
15+ // Now open the Option box.
16+ case Some (value: final sound):
17+ print (' -> Success: Sound setting is "$sound "\n ' );
18+ case None ():
19+ print (' -> Success: Sound setting was not specified.\n ' );
20+ }
21+ case Err err:
22+ // The entire pipeline failed at some point.
23+ print (' -> Failure: An error occurred: ${err .error }\n ' );
24+ }
25+ }
26+ }
27+
428typedef KeyValueMap = Map <String , dynamic >;
529
630// A network call that can fail. Async handles both success and exceptions.
@@ -23,8 +47,7 @@ Async<String> fetchUserData(int userId) => Async(() async {
2347 });
2448
2549// A parser that can fail. Sync automatically catches the jsonDecode exception.
26- Sync <KeyValueMap > parseJson (String json) =>
27- Sync (() => jsonDecode (json) as KeyValueMap );
50+ Sync <KeyValueMap > parseJson (String json) => Sync (() => jsonDecode (json) as KeyValueMap );
2851
2952// A helper to safely extract a typed value. It cannot fail, it can only be absent,
3053// so it returns an Option.
@@ -57,27 +80,3 @@ Async<Option<String>> getUserNotificationSound(int userId) {
5780 ),
5881 );
5982}
60-
61- void main () async {
62- for (var id in [1 , 2 , 3 , 4 , 5 ]) {
63- print ('Processing User ID: $id ' );
64-
65- // Execute the pipeline. `await value` opens the Async box.
66- final finalResult = await getUserNotificationSound (id).value;
67-
68- switch (finalResult) {
69- case Ok (value: final optionSound):
70- switch (optionSound) {
71- // Success! The value is an Option<String>.
72- // Now open the Option box.
73- case Some (value: final sound):
74- print (' -> Success: Sound setting is "$sound "\n ' );
75- case None ():
76- print (' -> Success: Sound setting was not specified.\n ' );
77- }
78- case Err err:
79- // The entire pipeline failed at some point.
80- print (' -> Failure: An error occurred: ${err .error }\n ' );
81- }
82- }
83- }
0 commit comments