Bloc: Possible to get state variables without using BlocBuilder

Created on 24 Apr 2019  路  1Comment  路  Source: felangel/bloc

I have the following piece of code:

class VehicleLoaded extends VehicleState {
  final Vehicle vehicle;

  VehicleLoaded({this.vehicle}) : super([vehicle]);
}

Is possible to get the variable vehicle without the need for a _BlocBuilder_?

Thanks

question

Most helpful comment

Hi @esfomeado 馃憢
Thanks for opening an issue!

You can access the stream of bloc states via the state property like:

bloc.state.listen((state) {
  // Do stuff with state
});

You can also access it from the UI using BlocListener:

BlocListener(
  bloc: bloc,
  listener: (BuildContext context, State state) {
    // Do stuff with state
  }
  child: ...
)

Alternatively, if you just want to know the current state of the bloc you can use the currentState property.

final state = bloc.currentState;
// Do stuff with state

Hope that helps! 馃憤

>All comments

Hi @esfomeado 馃憢
Thanks for opening an issue!

You can access the stream of bloc states via the state property like:

bloc.state.listen((state) {
  // Do stuff with state
});

You can also access it from the UI using BlocListener:

BlocListener(
  bloc: bloc,
  listener: (BuildContext context, State state) {
    // Do stuff with state
  }
  child: ...
)

Alternatively, if you just want to know the current state of the bloc you can use the currentState property.

final state = bloc.currentState;
// Do stuff with state

Hope that helps! 馃憤

Was this page helpful?
0 / 5 - 0 ratings