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:
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
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 ๐
Most helpful comment
Hi @reinaldonomoreira ๐
Thanks for opening an issue!
This is happening because you are stubbing
initialStaterather thanstate. 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 ๐