This code works
class CounterBloc extends Bloc<CounterEvent, int>
{
@override
int get initialState
=> 0;
@override
Stream<int> mapEventToState(CounterEvent event)
async* {
switch(event)
{
case CounterEvent.decrement:
yield currentState - 1;
break;
case CounterEvent.increment:
yield currentState + 1;
break;
}
}
}
but code below does not works
class CounterState
{
int value = 0;
}
class CounterBloc extends Bloc<CounterEvent, CounterState>
{
@override
CounterState get initialState
=> CounterState();
@override
Stream<CounterState> mapEventToState(CounterEvent event)
async* {
switch(event)
{
case CounterEvent.decrement:
yield currentState..value -= 1;
break;
case CounterEvent.increment:
yield currentState..value += 1;
break;
}
}
}
i am not able to figure out why.
mapEventToState is a method that must be implemented when a class extends Bloc. The function takes the incoming event as an argument. mapEventToState is called whenever an event is dispatched by the presentation layer. mapEventToState must convert that event into a new state and return the new state in the form of a Stream which is consumed by the presentation layer.
sorry i just read new state must be created
Most helpful comment
sorry i just read new state must be created