-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path032_extra.dart
109 lines (85 loc) · 2.55 KB
/
032_extra.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// ignore_for_file: unused_local_variable
import 'dart:collection'; // IterableBase library
import 'dart:async';
class Process {
// Represents a process...
}
class ProcessIterator implements Iterator<Process> {
final List<Process> _processes;
int _currentIndex = 0;
ProcessIterator(this._processes);
@override
Process get current => _processes[_currentIndex];
@override
bool moveNext() {
_currentIndex++;
return _currentIndex < _processes.length;
}
}
// A mythical class that lets you iterate through all
// processes. Extends a subclass of [Iterable].
class Processes extends IterableBase<Process> {
final List<Process> _processList;
Processes(this._processList);
@override
Iterator<Process> get iterator => ProcessIterator(_processList);
}
// Yineleme
void foo1() {
// Create a list of processes
final List<Process> processList = [
Process(), // Add your processes here
Process(),
// Add more processes as needed
];
// Create a Processes object with the list of processes
final processes = Processes(processList);
// Iterable objects can be used with for-in.
for (final process in processes) {
// Do something with the process.
print("${process.toString()}");
}
}
// My own "Exception" create
class FooException implements Exception {
final String? msg;
const FooException([this.msg]);
@override
String toString() => msg ?? 'FooException';
}
class MyException implements Exception {
final String? msg;
MyException([this.msg]);
@override
String toString() => msg ?? 'MyException';
}
void foo2() {
if (true) {
// throw FooException('This is a FooException');
throw MyException('This is a MyException');
}
}
Future<void> delete() async {
// Simulate a successful delete operation.
}
Future<void> copy() async {
// Simulate a successful copy operation.
}
Future<void> errorResult() async {
// Simulate an error result.
throw Exception('Error occurred during the operation');
}
void foo3() async {
try {
// Wait for each future in a list, returns a list of futures:
var results = await [delete(), copy(), errorResult()].wait;
} on ParallelWaitError<List<bool?>, List<AsyncError?>> catch (e) {
print(e.values[0]); // Prints successful future
print(e.values[1]); // Prints successful future
print(e.values[2]); // Prints null when the result is an error
print(e.errors[0]); // Prints null when the result is successful
print(e.errors[1]); // Prints null when the result is successful
print(e.errors[2]); // Prints error
}
}
void main() => foo3();