mirror of
https://github.com/HChaZZY/PiliPlus.git
synced 2025-12-06 09:13:48 +08:00
opt: sealed LoadingState (#765)
This commit is contained in:
committed by
GitHub
parent
d4d1602b45
commit
bb6bd95e9b
@@ -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';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +102,6 @@ class SearchHttp {
|
||||
case SearchType.article:
|
||||
data = SearchArticleModel.fromJson(res.data['data']);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return LoadingState.success(data);
|
||||
} catch (err) {
|
||||
|
||||
Reference in New Issue
Block a user