Sealed Classes & Status Handling Refactor
### ✨ New Features
* **Sealed Classes for Status Handling**
* Refactored `GetStatus` to use Dart’s sealed classes for improved type safety and pattern matching
* Added **exhaustive pattern matching** with the `match` method
* Added **`when` method** for side-effect-based status handling
* Added **`mapSuccess`** for transforming success values
* Added **`dataOrNull`** and **`errorOrNull`** extensions for safer data access
### 🔧 Improvements
* **Type Safety**: Stronger typing across state management APIs
* **Documentation**: Comprehensive docs for all public APIs
* **Testing**: Extended test coverage for new sealed class implementation
* **Performance**: Optimized state updates with efficient equality checks
### ⚠️ Breaking Changes
* `GetStatus` is now a **sealed class** with a private constructor
* `ErrorStatus` now accepts **only one type parameter**
* Custom status implementations must now **extend predefined status classes**
* `isCustom` getter only returns `true` for `CustomStatus` instances
### 🛠 Migration Guide
#### 1. Pattern Matching
Replace `if-else` chains with `match`:
```dart
// Before
if (status.isLoading) {
return LoadingWidget();
} else if (status.isError) {
return ErrorWidget(status.errorMessage);
} else {
return SuccessWidget(status.data);
}
// After
return status.match(
loading: () => LoadingWidget(),
error: (error) => ErrorWidget(error?.toString() ?? 'Unknown error'),
success: (data) => SuccessWidget(data),
empty: () => EmptyWidget(),
custom: () => CustomWidget(),
);
```
#### 2. Error Status
Update usages of `ErrorStatus` with **two type parameters** → now only **one**:
```dart
// Before
ErrorStatus<MyData, Exception> status;
// After
ErrorStatus<MyData> status;
```
#### 3. Custom Status
Custom implementations should now **extend predefined status classes**.
---
✅ **This PR makes state handling safer, more expressive, and future-proof.**
合并状态:未合并 关闭于 2025-10-02 0 条评论