Flutter Bloc Test:用于空值的空检查运算符(空安全)

时间:2021-06-14 05:05:09

标签: flutter bloc flutter-test flutter-bloc bloc-test

我正在使用 flutter_bloc、bloc_test 和 bloc_testing 来实现测试。但即使在我在 Setup 中初始化 Bloc 之后,错误仍然显示 Null 检查运算符用于空值。

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());
}

flutter test --no-sound-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 个答案:

答案 0 :(得分:0)

firbase 应用程序未初始化,因为它的未来需要将其包装在异步中

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