state_graph_bloc 0.0.4 copy "state_graph_bloc: ^0.0.4" to clipboard
state_graph_bloc: ^0.0.4 copied to clipboard

outdated

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

example/main.dart

import 'package:state_graph_bloc/state_graph_bloc.dart';

// Enumerate states.

abstract class DummyState {}

class StateInitialising extends DummyState {
  @override
  bool operator ==(Object other) => hashCode == other.hashCode;

  @override
  int get hashCode => runtimeType.hashCode;
}

class StateLoading extends DummyState {
  @override
  bool operator ==(Object other) => hashCode == other.hashCode;

  @override
  int get hashCode => runtimeType.hashCode;
}

class StateReady extends DummyState {
  StateReady(this.counter);

  final int counter;

  @override
  bool operator ==(Object other) => hashCode == other.hashCode;

  @override
  int get hashCode => runtimeType.hashCode ^ counter.hashCode;
}

// Enumerate events

abstract class DummyEvent {}

class LoadEvent extends DummyEvent {}

class LoadingCompletedEvent extends DummyEvent {}

class LoadingErrorEvent extends DummyEvent {}

class IncrementEvent extends DummyEvent {}

class SideEffectCauseEvent extends DummyEvent {}

class RestartEvent extends DummyEvent {}

// Define bloc with graph transitions

class DummyBloc extends StateGraphBloc<DummyEvent, DummyState> {
  @override
  DummyState get initialState => StateInitialising();

  @override
  StateGraph<DummyEvent, DummyState> buildGraph() =>
      StateGraph<DummyEvent, DummyState>(
        {
          StateInitialising: {
            LoadEvent: transitionWithSideEffect(
              (state, event) => StateLoading(),
              (state, event) {
                // There should be some fetching
                add(LoadingCompletedEvent());
              },
            ),
          },
          StateLoading: {
            LoadingCompletedEvent: transition((state, event) => StateReady(0)),
          },
          StateReady: {
            IncrementEvent: transition(
              (state, event) => StateReady((state as StateReady).counter + 1),
            ),
            // Test global event override.
            RestartEvent: transition((state, event) => StateReady(0)),
          },
        },
        {
          RestartEvent: transition((state, event) {
            return StateInitialising();
          })
        },
      );

  Stream<bool> isLoading() => bindState((state) {
        if (state is StateLoading) {
          return true;
        } else {
          return false;
        }
      });
}

// Listen to BLoC state.

void main() {
  final bloc = DummyBloc();
  
  // Access current state.
  bloc.state;

  // Listen to bloc (stream) updates. 
  // Remember to close when not needed (e.g. on bloc close).
  bloc.listen((state) => print("On state change"));

  // Listen to bloc field updates.
  // Remember to close when not needed (e.g. on bloc close).
  bloc.isLoading().listen((isLoading) => print("Is currently loading: $isLoading"));

  // Add events.
  bloc.add(LoadEvent());
}
8
likes
0
pub points
0%
popularity

Publisher

verified publisherjelenski.net

A bloc package with concise state graph builder. Build transitions, side effects and global events in a centralised, type-safe manner.

Repository (GitLab)
View/report issues

License

unknown (LICENSE)

Dependencies

bloc, rxdart

More

Packages that depend on state_graph_bloc