Sealed Classes & Status Handling Refactor#3396
Closed
Aniketkhote wants to merge 3 commits intojonataslaw:masterfrom
Aniketkhote:master
Closed
Sealed Classes & Status Handling Refactor#3396Aniketkhote wants to merge 3 commits intojonataslaw:masterfrom Aniketkhote:master
Aniketkhote wants to merge 3 commits intojonataslaw:masterfrom
Aniketkhote:master
Conversation
hashirshoaeb
reviewed
Sep 1, 2025
Comment on lines
+15
to
+20
| // Factory constructors for each variant | ||
| const factory GetStatus.loading() = LoadingStatus<T>._; | ||
| const factory GetStatus.error([Object? error]) = ErrorStatus<T>._; | ||
| const factory GetStatus.empty() = EmptyStatus<T>._; | ||
| const factory GetStatus.success(T data) = SuccessStatus<T>._; | ||
| const factory GetStatus.custom() = CustomStatus<T>._; |
There was a problem hiding this comment.
API does not match the previous options.
const factory RxStatus.loading() = RxLoading;
const factory RxStatus.loadingMore() = RxLoadingMore;
const factory RxStatus.success() = RxSuccess;
const factory RxStatus.error([String? message]) = RxError;
const factory RxStatus.empty() = RxEmpty;loadmore is missing. and custom is new.
error accept string not object.
Comment on lines
+39
to
+49
| /// Returns the data if this is a [SuccessStatus], otherwise returns null | ||
| T? get dataOrNull => switch (this) { | ||
| SuccessStatus(data: var d) => d, | ||
| _ => null, | ||
| }; | ||
|
|
||
| /// Returns the error if this is an [ErrorStatus], otherwise returns null | ||
| Object? get errorOrNull => switch (this) { | ||
| ErrorStatus(error: var e) => e, | ||
| _ => null, | ||
| }; |
Comment on lines
+110
to
+138
| extension GetStatusExt<T> on GetStatus<T> { | ||
| /// Returns the data if this is a [SuccessStatus], otherwise returns [defaultValue] | ||
| T dataOr(T defaultValue) => switch (this) { | ||
| SuccessStatus(data: var d) => d, | ||
| _ => defaultValue, | ||
| }; | ||
|
|
||
| /// Transforms the data if this is a [SuccessStatus], otherwise returns the same status | ||
| GetStatus<R> mapSuccess<R>(R Function(T) transform) => switch (this) { | ||
| SuccessStatus(data: var d) => GetStatus.success(transform(d)), | ||
| LoadingStatus() => GetStatus.loading(), | ||
| ErrorStatus(error: var e) => GetStatus.error(e), | ||
| EmptyStatus() => GetStatus.empty(), | ||
| CustomStatus() => GetStatus.custom(), | ||
| }; | ||
|
|
||
| /// Executes the appropriate callback based on the status and returns the result | ||
| R when<R>({ | ||
| R Function()? loading, | ||
| R Function(Object? error)? error, | ||
| R Function()? empty, | ||
| R Function(T data)? success, | ||
| R Function()? custom, | ||
| }) { | ||
| return switch (this) { | ||
| LoadingStatus() => | ||
| loading?.call() ?? (throw StateError('No handler for loading state')), | ||
| ErrorStatus(error: var e) => | ||
| error?.call(e) ?? (throw StateError('No handler for error state')), |
There was a problem hiding this comment.
i like the way you think. but i feel this is too much apis.
Contributor
Author
There was a problem hiding this comment.
So, are you suggesting we should just keep the existing APIs as they are and mainly add sealed, without making bigger changes?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✨ New Features
Sealed Classes for Status Handling
GetStatusto use Dart’s sealed classes for improved type safety and pattern matchingmatchmethodwhenmethod for side-effect-based status handlingmapSuccessfor transforming success valuesdataOrNullanderrorOrNullextensions for safer data access🔧 Improvements
GetStatusis now a sealed class with a private constructorErrorStatusnow accepts only one type parameterisCustomgetter only returnstrueforCustomStatusinstances🛠 Migration Guide
1. Pattern Matching
Replace
if-elsechains withmatch:2. Error Status
Update usages of
ErrorStatuswith two type parameters → now only one:3. Custom Status
Custom implementations should now extend predefined status classes.
✅ This PR makes state handling safer, more expressive, and future-proof.