Bloc: First emission of state is null in BlocBuilder

Created on 27 Jan 2020  ยท  3Comments  ยท  Source: felangel/bloc

Describe the bug
While testing, first emission of BlocBuilder is null (only when testing). This force me to do a check-up in my widget like if(state == null) return Container() in my code. Please, check the code attached for better understanding while reproducing it.

To Reproduce
Steps to reproduce the behaviour:

  1. Run the code sent attached or in the end of this issue
  2. Just run the test, it will throw a exception by this code in the widget (lines 21 to 24)
    if (state == null) { // return Container(); throw Exception('state is null'); }

*Logs *
โฏ flutter analyze Analyzing fluttebloc... No issues found! (ran in 2.2s)

Paste the output of running flutter doctor -v here.
`โฏ flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[โœ“] Flutter (Channel unknown, v1.10.14, on Mac OS X 10.14.6 18G103, locale en-BR)
[โœ“] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[โœ“] Xcode - develop for iOS and macOS (Xcode 10.1)
[โœ“] Android Studio (version 3.6)
[โœ“] Android Studio
[โœ“] IntelliJ IDEA Community Edition (version 2019.3)
[โœ“] Connected device (1 available)

โ€ข No issues found!
`

Additional context

Code:

Gist
or

fb.zip

bloc_test question

Most helpful comment

Hi @reinaldonomoreira ๐Ÿ‘‹
Thanks for opening an issue!

This is happening because you are stubbing initialState rather than state. Check out https://gist.github.com/reinaldomoreira/98e6d8ce1c296defe66fb064bbc2733b#gistcomment-3157637 for the updated test code.

Hope that helps and sorry for the inconvenience!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation ๐Ÿ‘

All 3 comments

I guess this line is not needed when(testBloc.initialState).thenReturn(TestState('Initial mocked'));

You can pass your initialState to whenListen, like that:

whenListen<TestEvent, TestState>(
  testBloc,
  Stream.fromIterable([
    TestState('Initial mocked')
    TestState('First emission'),
    TestState('Second emission'),
  ]),
);

I guess this line is not needed when(testBloc.initialState).thenReturn(TestState('Initial mocked'));

You can pass your initialState to whenListen, like that:

whenListen<TestEvent, TestState>(
  testBloc,
  Stream.fromIterable([
    TestState('Initial mocked')
    TestState('First emission'),
    TestState('Second emission'),
  ]),
);

Thanks for the reply! I understand it is not needed and I don't mock the initialState, it's there just for exhausting all the possible problems

Hi @reinaldonomoreira ๐Ÿ‘‹
Thanks for opening an issue!

This is happening because you are stubbing initialState rather than state. Check out https://gist.github.com/reinaldomoreira/98e6d8ce1c296defe66fb064bbc2733b#gistcomment-3157637 for the updated test code.

Hope that helps and sorry for the inconvenience!

Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation ๐Ÿ‘

Was this page helpful?
0 / 5 - 0 ratings