Skip to content

Instantly share code, notes, and snippets.

@edwardaux
Created October 3, 2018 04:37
Show Gist options
  • Save edwardaux/ae919c5d53d6349c371062db374cc4a3 to your computer and use it in GitHub Desktop.
Save edwardaux/ae919c5d53d6349c371062db374cc4a3 to your computer and use it in GitHub Desktop.
A hacky way to create Dart enum's with associated values
// 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