Skip to content

Commit b1b1628

Browse files
committed
improves doc from README
1 parent 9088003 commit b1b1628

File tree

1 file changed

+49
-11
lines changed

1 file changed

+49
-11
lines changed

README.md

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@ Result package for dart inspired by the work of [dartz](https://pub.dev/packages
55
This package is perfect to those of you who just want the Multiple results
66
functionality from dartz. 👌
77

8+
##### Old version:
9+
10+
If you're looking for a non null-safety version, you can find it in [here](https://github.com/higorlapa/result/tree/no-null-safety)
11+
12+
813
## How to use it
914

1015
In the return of a function, set it to return a Result type;
@@ -23,13 +28,13 @@ Result<Exception, String> getSomethingPretty() {
2328

2429
in return of the function, you just need to return
2530
```dart
26-
return Success("Something Pretty");
31+
return Success('Something Pretty');
2732
```
2833

2934
or
3035

3136
```dart
32-
return Error(Exception("something ungly happened..."));
37+
return Error(Exception('something ugly happened...'));
3338
```
3439

3540
The function should look something like this:
@@ -38,9 +43,9 @@ The function should look something like this:
3843
3944
Result<Exception, String> getSomethingPretty() {
4045
if(isOk) {
41-
return Success("OK!");
46+
return Success('OK!');
4247
} else {
43-
return Error(Exception("Not Ok!"));
48+
return Error(Exception('Not Ok!'));
4449
}
4550
}
4651
@@ -51,13 +56,16 @@ Result<Exception, String> getSomethingPretty() {
5156
```dart
5257
void main() {
5358
final result = getSomethingPretty();
54-
result.when((error) {
55-
// handle the error here
56-
print(error);
57-
}, (success) {
58-
// handle the success here
59-
print(success);
60-
});
59+
final String message = result.when(
60+
(error) {
61+
// handle the error here
62+
return "error";
63+
}, (success) {
64+
// handle the success here
65+
return "success";
66+
},
67+
);
68+
6169
}
6270
```
6371

@@ -75,3 +83,33 @@ void main() {
7583
}
7684
```
7785

86+
87+
#### Handling the Result with `getSuccess`
88+
89+
```dart
90+
void main() {
91+
final result = getSomethingPretty();
92+
93+
String? mySuccessResult;
94+
if (result.isSuccess()) {
95+
mySuccessResult = result.getSuccess();
96+
}
97+
}
98+
99+
```
100+
101+
102+
#### Handling the Result with `getError`
103+
104+
```dart
105+
void main() {
106+
final result = getSomethingPretty();
107+
108+
Exception? myException;
109+
if (result.isError()) {
110+
myException = result.getError();
111+
}
112+
}
113+
```
114+
115+

0 commit comments

Comments
 (0)