union 0.0.2 copy "union: ^0.0.2" to clipboard
union: ^0.0.2 copied to clipboard

discontinuedreplaced by: freezed
outdated

Type safe union types for dart, by using extensions from Dart 2.6

example/example.dart

import 'package:union/union.dart';

class Loading {
  const Loading();
}

class AsyncState<T> extends Union3<T, Loading, Exception> {
  const AsyncState.value(T value) : super.first(value);
  const AsyncState.loading() : super.second(const Loading());
  const AsyncState.exception(Exception error) : super.third(error);
}

extension ToAsyncState<T> on Union3<T, Loading, Exception> {
  AsyncState<T> toAsyncState() {
    return join(
      (v) => AsyncState.value(v),
      (_) => const AsyncState.loading(),
      (v) => AsyncState.exception(v),
    );
  }
}

void main() {
  var state = const AsyncState<int>.value(42);
  state = state
      .map(
        (v) => v * 2,
        (v) => v,
        (v) => v,
      )
      .toAsyncState();
}
20
likes
0
pub points
60%
popularity

Publisher

verified publisherdash-overflow.net

Type safe union types for dart, by using extensions from Dart 2.6

Repository (GitHub)
View/report issues

License

unknown (LICENSE)

More

Packages that depend on union