Flutter Bloc Test : Null check operator used on a null value (null-safety)

675 Views Asked by At

I am using flutter_bloc, bloc_test, and bloc_testing to implement testing. But even after I initialize Bloc in Setup the errors say Null check operator used on a null value.

import 'package:bloc_test/bloc_test.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:mockito/mockito.dart';
import 'package:orotask/logic/bloc/map_bloc/map_bloc.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:orotask/logic/repository/addr_map_repository.dart';

class MockAddrRepository extends Mock implements MapAddrRepository {}

void main()async {

  late MapAddrRepository mapAddrRepository ;
  late MapBloc mapBloc;
  await Firebase.initializeApp();

  setUp(() {
     
    mapAddrRepository = MapAddrRepository();
    mapBloc = MapBloc();
  });

  tearDown(() {
    mapBloc.close();
  });

  blocTest<MapBloc, MapState>('Map Bloc Initial State Tests',
      build: () => mapBloc,
      expect: () => MapNotLoaded());
}

Error in flutter test --no-sound-null-safety (I have some dependencies which don't support null-safety)

00:20 +0 -1: loading C:\Users\Dell\Desktop\OroTask\orotask\test\src\bloc\visit_bloc\visit_bloc_test.dart [E]                                                   
  Failed to load "C:\Users\Dell\Desktop\OroTask\orotask\test\src\bloc\visit_bloc\visit_bloc_test.dart": Null check operator used on a null value
  package:flutter/src/services/platform_channel.dart 142:86                                       MethodChannel.binaryMessenger
  package:flutter/src/services/platform_channel.dart 148:36                                       MethodChannel._invokeMethod
  package:flutter/src/services/platform_channel.dart 331:12                                       MethodChannel.invokeMethod
  package:flutter/src/services/platform_channel.dart 344:41                                       MethodChannel.invokeListMethod
  package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart 30:37  MethodChannelFirebase._initializeCore
  package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart 77:13  MethodChannelFirebase.initializeApp
  package:firebase_core/src/firebase.dart 41:47                                                   Firebase.initializeApp
  test\src\bloc\visit_bloc\visit_bloc_test.dart 

14:18

1

There are 1 best solutions below

2
On

the firbase app is not intalised as its future you need to wrap it in async

void main() async {
 await Firebase.initializeApp();}