I have a flutter bloc_test which is failing due to recent upgrades in Flutter involving null safety.
I have the following code
blocTest('get the usecase name',
build: () {
when(() => mockGetUseCaseName(any()))
.thenAnswer((_) async => Right(name));
return bloc;
},
act: (bloc) => bloc.add(GetUseCaseName(name)),
verify: (_) => verify(
() => mockGetUseCaseName(Params(string: name))));
And I get the error on the line "bloc.add"
"The method 'add' can't be unconditionally invoked because the receiver can be 'null'"
Any ideas?
I found the answer - for any others out there with the same issue. I fixed it by casting the bloc type (because act/bloc is of Object? type)
act: (bloc) => cast<UseCaseBloc>(bloc).add(GetUseCaseName(name))
Have a good day.
Kia Kaha,
Mike Smith
the most correct solution would be to make the data type explicit
Before:
blocTest('get the usecase name',
build: () {
when(() => mockGetUseCaseName(any()))
.thenAnswer((_) async => Right(name));
return bloc;
},
act: (bloc) => bloc.add(GetUseCaseName(name)),
verify: (_) => verify(
() => mockGetUseCaseName(Params(string: name))));
after:
blocTest<ClassBloc, ClassState>('get the usecase name',
build: () {
when(() => mockGetUseCaseName(any()))
.thenAnswer((_) async => Right(name));
return bloc;
},
act: (bloc) => bloc.add(GetUseCaseName(name)),
verify: (_) => verify(
() => mockGetUseCaseName(Params(string: name))));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.