Bloc: i cant make object values to work in provide example

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

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.

Most helpful comment

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

>All comments

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

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobPFarley picture RobPFarley  路  3Comments

krusek picture krusek  路  3Comments

tigranhov picture tigranhov  路  3Comments

komapeb picture komapeb  路  3Comments

rsnider19 picture rsnider19  路  3Comments