Open flutternoob opened 3 years ago
It seems to me that you specified the program may expect null reference object on type CounterCubit. Change "CounterCubit? counterCubit" to "late CounterCubit counterCubit", and replace any "counterCubit!" occurrences to "counterCubit" should work.
https://medium.com/flutterworld/flutter-null-safety-5d20012c2441
group('CounterCubit', () {
late CounterCubit counterCubit;
setUp((){
counterCubit = CounterCubit();
});
tearDown((){
counterCubit.close();
});
test('the initial state for the CounterCubit is CounterState(counterValue:0)', () {
expect(counterCubit.state, CounterState(counterValue: 0, wasIncremented: false));
});
blocTest('the cubit should emit a CounterState(counterValue: 1, wasIncremented:true) when cubit.increment() function is called',
build: () => counterCubit,
act: (cubit) => counterCubit.increment(),
expect: () => [CounterState(counterValue: 1, wasIncremented: true)],
);
blocTest('the cubit should emit a CounterState(counterValue: -1, wasIncremented:false) when cubit.decrement() function is called',
build: () => counterCubit,
act: (cubit) => counterCubit.decrement(),
expect: () => [CounterState(counterValue: -1, wasIncremented: false)],
);
});
I am trying to run the code with null safety enabled in the project. I keep getting the following error when I run the test on
counter_cubit_test.dart
:Null check operator used on a null value
This is the code in the
counter_cubit.dart
file:This is the code in the
counter_state.dart
file:This is the code in the
main.dart
file:This is the code in the
counter_cubit_test.dart
file: