state not yield in Flutter bloc pattern

Issue

I am building a test app in flutter bloc patter using flutter_bloc package. The problem is only the states of Event1 are rendered but the code executes in mapEventToState. I think the problem is with the yield statement because on triggering the Event2 the corresponding print statements in mapEventToState Executes.

The mapEventTOState function in page_bloc.dart

Stream<PageState> mapEventToState(PageEvent event) async* {
    print(event);
    if (event is Event1) {
      print(">> $event");
      yield State10();
      try {
        await someAsyncFunction();
        yield State11(message: "message");
        print("reached 100");
      } catch (e) {
        yield State12();
      }
    } else if (event is Event2) {
      yield State20();
      try {
        await someAyncFunction2();
        yield State21();
      } catch (e) {
        yield State22(message: e.toString());
      }
    }
    // TODO: implement mapEventToState
  }

The page_state.dart is as follows:

abstract class PageState extends Equatable {
  @override
  List<Object> get props => [];
}

class State10 extends PageState {}
class State11 extends PageState {}
class State12 extends PageState {}

class State20 extends PageState {}
class State21 extends PageState {}
class State22 extends PageState {}

The page_event.dart is given below

abstract class PageEvent extends Equatable {
  @override
  // TODO: implement props
  List<Object> get props => null;
}

class Event1 extends PageEvent {}
class Event2 extends PageEvent {}

The BlocBuilder is as follows:

child: BlocBuilder<PageBloc, PageState>(
                    builder: (context, state) {
                      print(state);
                      if (state is State10) {
                        return Text("State10");
                      } else if (state is State11) {
                        return Text("State11");
                      } else if (state is State12) {
                        return Text("State12");
                      } else if (state is State20) {
                        return Text("State20");
                      } else if (state is State21) {
                        return Text("State21");
                      } else if (state is State22) {
                        return Text("State22");
                      }                       
                    },
                  )

The floating action button in Scaffold is as follows:

floatingActionButton: FloatingActionButton(
        onPressed: () => pageBlocInstace2..add(Event2()),
        child: Icon(Icons.done),
      ),

And another widget in the scaffold is as follows:

GestureDetector(
        onTap: () => pageBlocInstace1..add(Event1()),
        child: Center(child: Text("Tap to here")));

Solution

This issue was due to the fact that the two events call separate bloc instance.

floatingActionButton: FloatingActionButton(
        onPressed: () => pageBlocInstace..add(Event2()),
        child: Icon(Icons.done),
      ),

GestureDetector(
        onTap: () => pageBlocInstace..add(Event1()),
        child: Center(child: Text("Tap to here")));

Answered By – Abhijith K

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.