Skip to content

Commit 58f4bdf

Browse files
committed
feat: add onSuccess and onError; refactor: rename add try nomenclature to tryGetSuccess and tryGetError
1 parent 67a231e commit 58f4bdf

File tree

6 files changed

+176
-64
lines changed

6 files changed

+176
-64
lines changed

README.md

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ In the return of a function, set it to return a Result type;
1616
```dart
1717
Result getSomethingPretty();
1818
```
19-
then add the Error and the Success types.
19+
then add the Success and the Error types.
2020

2121
```dart
2222
23-
Result<Exception, String> getSomethingPretty() {
23+
Result<String, Exception> getSomethingPretty() {
2424
2525
}
2626
@@ -41,7 +41,7 @@ The function should look something like this:
4141

4242
```dart
4343
44-
Result<Exception, String> getSomethingPretty() {
44+
Result<String, Exception> getSomethingPretty() {
4545
if(isOk) {
4646
return Success('OK!');
4747
} else {
@@ -69,9 +69,32 @@ void main() {
6969
}
7070
```
7171

72+
#### Handling the Result with `onSuccess` or `onError`
73+
74+
```dart
75+
final result = getSomethingPretty();
76+
// notice the [onSuccess] or [onError] will only be executed if
77+
// the result is a Success or an Error respectivaly.
78+
final output = result.onSuccess((name) {
79+
// handle here the success
80+
return "";
81+
});
82+
83+
final result = getSomethingPretty();
84+
85+
// [result] is NOT an Error, this [output] will be null.
86+
final output = result.onError((exception) {
87+
// handle here the error
88+
return "";
89+
});
90+
```
7291

7392
#### Handling the Result with `get`
7493

94+
```
95+
note: [get] is now deprecated and will be removed in the next version.
96+
```
97+
7598
```dart
7699
void main() {
77100
final result = getSomethingPretty();
@@ -84,30 +107,30 @@ void main() {
84107
```
85108

86109

87-
#### Handling the Result with `getSuccess`
110+
#### Handling the Result with `tryGetSuccess`
88111

89112
```dart
90113
void main() {
91114
final result = getSomethingPretty();
92115
93116
String? mySuccessResult;
94117
if (result.isSuccess()) {
95-
mySuccessResult = result.getSuccess();
118+
mySuccessResult = result.tryGetSuccess();
96119
}
97120
}
98121
99122
```
100123

101124

102-
#### Handling the Result with `getError`
125+
#### Handling the Result with `tryGetError`
103126

104127
```dart
105128
void main() {
106129
final result = getSomethingPretty();
107130
108131
Exception? myException;
109132
if (result.isError()) {
110-
myException = result.getError();
133+
myException = result.tryGetError();
111134
}
112135
}
113136
```

example/lib/main.dart

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,20 @@ class _MyHomePageState extends State<MyHomePage> {
5353
floatingActionButton: FloatingActionButton(
5454
onPressed: () {
5555
final result = _shouldIncrement(_counter);
56-
if (result.isSuccess()) {
57-
setState(() {
58-
_counter++;
59-
});
60-
} else {
61-
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text("Counter cannot be bigger than 9")));
62-
}
56+
result.when(
57+
(success) {
58+
setState(() {
59+
_counter++;
60+
});
61+
},
62+
(error) {
63+
ScaffoldMessenger.of(context).showSnackBar(
64+
SnackBar(
65+
content: Text(error.message),
66+
),
67+
);
68+
},
69+
);
6370
},
6471
tooltip: 'Increment',
6572
child: Icon(Icons.add),

example/pubspec.lock

Lines changed: 22 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ packages:
77
name: async
88
url: "https://pub.dartlang.org"
99
source: hosted
10-
version: "2.5.0"
10+
version: "2.9.0"
1111
boolean_selector:
1212
dependency: transitive
1313
description:
@@ -21,35 +21,28 @@ packages:
2121
name: characters
2222
url: "https://pub.dartlang.org"
2323
source: hosted
24-
version: "1.1.0"
25-
charcode:
26-
dependency: transitive
27-
description:
28-
name: charcode
29-
url: "https://pub.dartlang.org"
30-
source: hosted
31-
version: "1.2.0"
24+
version: "1.2.1"
3225
clock:
3326
dependency: transitive
3427
description:
3528
name: clock
3629
url: "https://pub.dartlang.org"
3730
source: hosted
38-
version: "1.1.0"
31+
version: "1.1.1"
3932
collection:
4033
dependency: transitive
4134
description:
4235
name: collection
4336
url: "https://pub.dartlang.org"
4437
source: hosted
45-
version: "1.15.0"
38+
version: "1.16.0"
4639
fake_async:
4740
dependency: transitive
4841
description:
4942
name: fake_async
5043
url: "https://pub.dartlang.org"
5144
source: hosted
52-
version: "1.2.0"
45+
version: "1.3.1"
5346
flutter:
5447
dependency: "direct main"
5548
description: flutter
@@ -66,28 +59,35 @@ packages:
6659
name: matcher
6760
url: "https://pub.dartlang.org"
6861
source: hosted
69-
version: "0.12.10"
62+
version: "0.12.12"
63+
material_color_utilities:
64+
dependency: transitive
65+
description:
66+
name: material_color_utilities
67+
url: "https://pub.dartlang.org"
68+
source: hosted
69+
version: "0.1.5"
7070
meta:
7171
dependency: transitive
7272
description:
7373
name: meta
7474
url: "https://pub.dartlang.org"
7575
source: hosted
76-
version: "1.3.0"
76+
version: "1.8.0"
7777
multiple_result:
7878
dependency: "direct main"
7979
description:
8080
path: ".."
8181
relative: true
8282
source: path
83-
version: "1.0.1"
83+
version: "1.0.4"
8484
path:
8585
dependency: transitive
8686
description:
8787
name: path
8888
url: "https://pub.dartlang.org"
8989
source: hosted
90-
version: "1.8.0"
90+
version: "1.8.2"
9191
sky_engine:
9292
dependency: transitive
9393
description: flutter
@@ -99,7 +99,7 @@ packages:
9999
name: source_span
100100
url: "https://pub.dartlang.org"
101101
source: hosted
102-
version: "1.8.0"
102+
version: "1.9.0"
103103
stack_trace:
104104
dependency: transitive
105105
description:
@@ -120,34 +120,27 @@ packages:
120120
name: string_scanner
121121
url: "https://pub.dartlang.org"
122122
source: hosted
123-
version: "1.1.0"
123+
version: "1.1.1"
124124
term_glyph:
125125
dependency: transitive
126126
description:
127127
name: term_glyph
128128
url: "https://pub.dartlang.org"
129129
source: hosted
130-
version: "1.2.0"
130+
version: "1.2.1"
131131
test_api:
132132
dependency: transitive
133133
description:
134134
name: test_api
135135
url: "https://pub.dartlang.org"
136136
source: hosted
137-
version: "0.2.19"
138-
typed_data:
139-
dependency: transitive
140-
description:
141-
name: typed_data
142-
url: "https://pub.dartlang.org"
143-
source: hosted
144-
version: "1.3.0"
137+
version: "0.4.12"
145138
vector_math:
146139
dependency: transitive
147140
description:
148141
name: vector_math
149142
url: "https://pub.dartlang.org"
150143
source: hosted
151-
version: "2.1.0"
144+
version: "2.1.2"
152145
sdks:
153-
dart: ">=2.12.0 <3.0.0"
146+
dart: ">=2.17.0-0 <3.0.0"

lib/multiple_result.dart

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,16 @@ abstract class Result<S, E> {
2424
/// ```
2525
///
2626
/// before casting the value;
27+
@Deprecated('Will be removed in the next version. '
28+
'Use `tryGetSuccess` or `tryGetError` instead.'
29+
'You may also use `onSuccess` and on `onError` for similar result.')
2730
dynamic get();
2831

29-
/// Returns the value of [S].
30-
S? getSuccess();
32+
/// Returns the value of [S] if any.
33+
S? tryGetSuccess();
3134

32-
/// Returns the value of [E].
33-
E? getError();
35+
/// Returns the value of [E] if any.
36+
E? tryGetError();
3437

3538
/// Returns true if the current result is an [Error].
3639
bool isError();
@@ -47,6 +50,16 @@ abstract class Result<S, E> {
4750
W Function(S success) whenSuccess,
4851
W Function(E error) whenError,
4952
);
53+
54+
/// Execute [whenSuccess] if the [Result] is a success.
55+
R? onSuccess<R>(
56+
R Function(S success) whenSuccess,
57+
);
58+
59+
/// Execute [whenError] if the [Result] is an error.
60+
R? onError<R>(
61+
R Function(E error) whenError,
62+
);
5063
}
5164

5265
/// Success Result.
@@ -78,7 +91,8 @@ class Success<S, E> implements Result<S, E> {
7891
int get hashCode => _success.hashCode;
7992

8093
@override
81-
bool operator ==(Object other) => other is Success && other._success == _success;
94+
bool operator ==(Object other) =>
95+
other is Success && other._success == _success;
8296

8397
@override
8498
W when<W>(
@@ -89,10 +103,18 @@ class Success<S, E> implements Result<S, E> {
89103
}
90104

91105
@override
92-
E? getError() => null;
106+
E? tryGetError() => null;
93107

94108
@override
95-
S? getSuccess() => _success;
109+
S tryGetSuccess() => _success;
110+
111+
@override
112+
R? onError<R>(R Function(E error) whenError) => null;
113+
114+
@override
115+
R onSuccess<R>(R Function(S success) whenSuccess) {
116+
return whenSuccess(_success);
117+
}
96118
}
97119

98120
/// Error Result.
@@ -133,25 +155,34 @@ class Error<S, E> implements Result<S, E> {
133155
}
134156

135157
@override
136-
E? getError() => _error;
158+
E tryGetError() => _error;
159+
160+
@override
161+
S? tryGetSuccess() => null;
162+
163+
@override
164+
R onError<R>(R Function(E error) whenError) => whenError(_error);
137165

138166
@override
139-
S? getSuccess() => null;
167+
R? onSuccess<R>(R Function(S success) whenSuccess) => null;
168+
140169
}
141170

142171
/// Default success class.
143172
///
144173
/// Instead of returning void, as
145174
/// ```dart
146-
/// Result<Exception, void>
175+
/// Result<void, Exception>
147176
/// ```
148177
/// return
149178
/// ```dart
150-
/// Result<Exception, SuccessResult>
179+
/// Result<SuccessResult, Exception>
151180
/// ```
152181
class SuccessResult {
153182
const SuccessResult._internal();
154183
}
155184

156185
/// Default success case.
157186
const success = SuccessResult._internal();
187+
188+

pubspec.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
name: multiple_result
22
description: Multiple results for dart. Inspired by dartz's Either and Kotlin's sealed classes
3-
version: 1.0.4
4-
homepage: https://github.com/higorlapa/result
3+
version: 2.0.0
4+
repository: https://github.com/higorlapa/result
55

66
environment:
7-
sdk: ">=2.12.0 <3.0.0"
7+
sdk: ">=2.15.1 <3.0.0"
88

99
dependencies:
1010
meta: ^1.3.0

0 commit comments

Comments
 (0)