-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy patheither_option_example.dart
More file actions
executable file
·36 lines (29 loc) · 1.02 KB
/
Copy patheither_option_example.dart
File metadata and controls
executable file
·36 lines (29 loc) · 1.02 KB
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
import 'package:either_option/either_option.dart';
import 'model/server_error.dart';
import 'model/user.dart';
import 'repository_example.dart';
/// You can combine all functions defined in src/either.dart and src/option.dart
main() async {
final Repository repository = Repository();
print("------Either Example---------");
final Either<ServerError, User> res = await repository.getUser(3);
final defaultUser = User(id: 0, username: "ko", password: "ko");
final userName = res.fold((_) => defaultUser.username, (user) => user.id);
print(userName); // "User 3"
print("---------Option Example---------");
final maybeUser = await repository.getUserOpt(345);
final userNone = maybeUser.getOrElse(() => defaultUser);
print(userNone); // User(0, ko, ko)
print("---------Option Example 2---------");
String f(A a) => a.toString();
final some = Option.of(B());
print(some.map((s) => f(s)));
}
class A {
@override
String toString() => "Im a A";
}
class B extends A {
@override
String toString() => "Im a B";
}