Created
October 3, 2018 04:37
-
-
Save edwardaux/ae919c5d53d6349c371062db374cc4a3 to your computer and use it in GitHub Desktop.
A hacky way to create Dart enum's with associated values
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
// Models an enum with three mutually exclusive states: | |
// - loading | |
// - success (with an object of some type) | |
// - error (with an Error object) | |
abstract class LoadingStatus<T> { | |
static const loading = const LoadingStatusLoading(); | |
factory LoadingStatus.success(T object) = LoadingStatusSuccess; | |
factory LoadingStatus.error(Error error) = LoadingStatusError; | |
} | |
class LoadingStatusLoading implements LoadingStatus { | |
const LoadingStatusLoading(); | |
} | |
class LoadingStatusSuccess<T> implements LoadingStatus<T> { | |
final T object; | |
const LoadingStatusSuccess(this.object); | |
} | |
class LoadingStatusError<T> implements LoadingStatus<T> { | |
final Error error; | |
LoadingStatusError(this.error); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment