I am experimenting on dart`s new feature sealed classes and using it on a switch statement. I get the compile time error: The type 'PossibleErrors' is not exhaustively matched by the switch cases. Try adding a default case or cases that match PossibleErrors.
I have defined a sealed class as follows:
sealed class PossibleErrors implements Exception {
void error(String e);
}
Afterwards I have declared 3 subclasses in the same file as follows:
class IntParseError extends PossibleErrors {
@override
void error(e) {
print(e);
}
}
class StringParseError extends PossibleErrors {
@override
void error(e) {
print(e);
}
}
class ListParseError extends PossibleErrors {
@override
void error(e) {
print(e);
}
}
Finally I have a switch statement to handle all the possible errors that can be thrown:
try {
throw IntParseError();
} on PossibleErrors catch (t, e) {
switch (t) {
case IntParseError _:
t.error("Int Parse Error");
case StringParseError _:
t.error("String Parse Error");
case ListParseError _:
t.error("List Parse Error");
}
}
The above switch statement does not compile yet all cases have been handled. Adding a default clause solves the problem but that nullifies the need to use the sealed class.[To make sure all sealed class SubClasses have been handled].
I am using the latest snapshot on the master channel. Heres my flutter --version output:
user@users-MacBook-Pro color-generator % flutter --version Flutter 3.9.0-7.0.pre.8 • channel master • https://github.com/flutter/flutter.git Framework • revision ac37dd2611 (21 hours ago) • 2023-03-14 08:14:19 -0400 Engine • revision 59e0ced91d Tools • Dart 3.0.0 (build 3.0.0-322.0.dev) • DevTools 2.22.2