-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
result-type-in-dart.dart
91 lines (80 loc) · 2.19 KB
/
result-type-in-dart.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
88
89
90
91
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'package:flutter/material.dart';
import 'dart:developer' as devtools show log;
extension Log on Object {
void log() => devtools.log(toString());
}
void main() {
runApp(
const App(),
);
}
class App extends StatelessWidget {
const App({
Key? key,
}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(
primarySwatch: Colors.blue,
),
debugShowCheckedModeBanner: false,
home: const HomePage(),
);
}
}
enum ResultType { value, error }
@immutable
class Result<T, E> {
final T? value;
final E? error;
final ResultType type;
const Result.error({required E input})
: error = input,
value = null,
type = ResultType.error;
const Result.value({required T input})
: value = input,
error = null,
type = ResultType.value;
E get requireError => error!;
T get requireValue => value!;
@override
String toString() {
switch (type) {
case ResultType.value:
return 'Value = ${requireValue.toString()}';
case ResultType.error:
return 'Error = ${requireError.toString()}';
}
}
}
void testIt() {
const valueResult = Result.value(input: 10);
valueResult.log(); // Value = 10
valueResult.value?.log(); // 10
valueResult.error?.log(); // no log occurs
const errorResult = Result.error(input: 404);
errorResult.log(); // Error = 404
errorResult.value?.log(); // no logs occur
errorResult.error?.log(); // 404
}
class HomePage extends StatelessWidget {
const HomePage({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
testIt();
return Scaffold(
appBar: AppBar(
title: const Text('Home Page'),
),
);
}
}