14

I'm relatively new to Flutter and the BLoC pattern, so I'm still trying to wrap my head around everything.

Let's say I have a quiz app where I have a BLoC called QuestionBloc which uses a repository to fetch questions from a file. Event on QuestionBloc

  • FetchQuestion

States on QuestionBloc

  • QuestionEmpty
  • QuestionLoading
  • QuestionLoaded which contains a question object
  • QuestionError

I then have another BLoC called QuestionValidatorBloc which is responsible for validating the answers to the question. The answer is entered into a text field and there is a submit button to trigger the validation. Event on QuestionValidatorBloc

  • ValidateQuestion

States on QuestionValidatorBloc

  • ValidateInitial
  • ValidateInProgress
  • ValidateSuccess
  • ValidateError

This is fairly straight forward. However, now I need to incorporate both QuestionBloc and QuestionValidatorBloc into the same widget since one of them is responsible for fetching and displaying the question and the other for handling the validation action. How can I achieve this?

hoan
  • 1,277
  • 3
  • 18
  • 32
  • What if you use just one BloC, and 2 or more subjects? You're using the same widget. So its better to have only one BLoC and streams builders as needed – Sergio Bernal Aug 01 '19 at 14:16
  • 2
    Split two different job in two bloc is positive imo. I saw a lot of peope told one bloc for one page, but I thought they didn't see more complex use case. In this case, `validation` and `api call` is absolutely different job, I don't know why use one is better adoption. – Tokenyet Sep 12 '19 at 04:29
  • What if I need another bloc, inside of this bloc? I'm talking about using 2+ bloc's inside of bloc. I would probably have 2 subscriptions, but how would I nest it? State listener in another state listener is bad approach :/ I'm stuck – fkvestak Nov 06 '19 at 14:01

1 Answers1

5

I assumed you are using the flutter_bloc library. Let the QuestionBloc listen to the QuestionValidatorBloc using StreamSubscription

class QuestionBloc extends Bloc<QuestionEvent, QuestionState> {
  QuestionValidatorBloc questionValidatorBloc;
  StreamSubscription subscription;

  MainPageBloc({@required this.questionValidatorBloc}) {
    subscription= questionValidatorBloc.state.listen((state) {
      if (state is ValidateSuccess) {
        dispatch(YourEvent());
      } else if(state is ValidateError{
        dispatch(AnotherEvent());
      }
    });
  }

...

Now you just have to pass the QuestionValidatorBloc to the QuestionBloc constructor.

Oliver Atienza
  • 631
  • 4
  • 8
  • 2
    What if I need another bloc, inside of this bloc? I'm talking about using 2+ bloc's inside of bloc. I would probably have 2 subscriptions, but how would I nest it? State listener in another state listener is bad approach :/ I'm stuck – fkvestak Nov 06 '19 at 14:00
  • @fkvestak Have you figured it out? – Anis Alibegić Jan 04 '21 at 11:22
  • @Spectarion no, i'm simply not using bloc anymore, you should try mobx – fkvestak Jan 07 '21 at 07:24
  • @AnisAlibegić since `bloc.state` is a `Stream` you could combine multiple streams together and then listen to a combination of streams as a single `Stream` object. if dart doesn't have anything for that rxdart library definitely has – alireza easazade Jun 21 '21 at 06:52