-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.dart
87 lines (69 loc) · 2.7 KB
/
example.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import 'dart:math';
import 'package:http/http.dart' as http;
import 'package:result_type/src/result.dart';
import 'imaginary_service.dart';
import 'photo.dart';
import 'photos_service.dart';
final client = http.Client();
final random = Random();
void main() async {
final photosResult = await PhotosService.getPhotos(client);
final photosOr = photosResult.unwrapOr([Photo.initial()]);
print(photosOr);
/// Use `switch` to handle both success and failure.
final _ = switch (photosResult) {
Success(value: final data) => data,
Failure(value: final error) => 'Error: $error'
};
/// Do something with successful operation results or handle an error.
if (photosResult.isSuccess) {
print('Photos Items: ${photosResult.success}');
} else {
print('Error: ${photosResult.failure}');
}
// Chain multiple asynchronous operations.
final result1 = await ImaginaryService.fetchData1();
final result2 = await ImaginaryService.fetchData2(result1.success);
final result3 = await ImaginaryService.fetchData2(result2.success);
// Print the result of the last operation: `Success: Default Data`
print(result3.unwrapOr('Default Data'));
// This will throw an exception as `_handleResult`
// has a case with 'Wrong Data'.
//print(result3.unwrap());
String length(String string) => string.length.toString();
final unwrapOrElse = result3.unwrapOrElse('Default (((((Data)))))', length);
// Print the result of the last operation: `22`
print(unwrapOrElse);
/// Apply transformation to successful operation results or handle an error.
if (photosResult.isSuccess) {
final items = photosResult
.map(
(i) => i.where(
(j) => j.title.length > 60,
),
)
.success;
print('Number of Long Titles: ${items.length}');
} else {
print('Error: ${photosResult.failure}');
}
/// In this example, note the difference in the result of using `map` and
/// `flatMap` with a transformation that returns an result type.
Result<int, Exception> getNextInteger() => Success(random.nextInt(4));
Result<int, Exception> getNextAfterInteger(int n) =>
Success(random.nextInt(n + 1));
final nextIntegerNestedResults = getNextInteger().map(getNextAfterInteger);
print(nextIntegerNestedResults.runtimeType);
// Prints: Success<Result<int, Error>, dynamic>
final nextIntegerUnboxedResults =
getNextInteger().flatMap(getNextAfterInteger);
print(nextIntegerUnboxedResults.runtimeType);
// Prints: Success<int, Error>
/// Use completion handler / callback style API if you want to.
await PhotosService.getPhotos(client)
..result((photos) {
// print('Photos: $photos');
}, (error) {
print('Error: $error');
});
}