opt: sealed LoadingState (#765)

This commit is contained in:
My-Responsitories
2025-04-27 22:17:36 +08:00
committed by GitHub
parent d4d1602b45
commit bb6bd95e9b
54 changed files with 16 additions and 61 deletions

View File

@@ -1,9 +1,19 @@
abstract class LoadingState<T> {
import 'dart:core' hide Error;
sealed class LoadingState<T> {
const LoadingState();
factory LoadingState.loading() = Loading;
factory LoadingState.success(T response) = Success<T>;
factory LoadingState.error(String errMsg) = Error;
bool get isSuccess => this is Success<T>;
T get data => switch (this) {
Success(response: final res) => res,
Error() => throw this,
Loading() => throw Exception('ApiException: loading'),
};
}
class Loading extends LoadingState<Never> {
@@ -50,4 +60,9 @@ class Error extends LoadingState<Never> {
@override
int get hashCode => errMsg.hashCode;
@override
String toString() {
return 'ApiException: $errMsg';
}
}