Flutter Bloc Test:空值检查运算符用于空值(空安全)
Posted
技术标签:
【中文标题】Flutter Bloc Test:空值检查运算符用于空值(空安全)【英文标题】:Flutter Bloc Test : Null check operator used on a null value (null-safety) 【发布时间】:2021-08-30 01:15:07 【问题描述】:我正在使用flutter_bloc、bloc_test和bloc_testing来实现测试。但即使在我在设置中初始化 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】:firbase 应用程序没有集成为它的未来,您需要将其包装在异步中
void main() async
await Firebase.initializeApp();
【讨论】:
感谢 Piyush 的帮助,我在 Setup 中尝试了异步,但在 main 中没有。虽然这有效,但我仍然得到一个空错误。我已经用新的错误消息更新了问题,如果您也能帮助我解决这个问题,我将不胜感激! 问题在于您的 bloc 库尝试降级它以上是关于Flutter Bloc Test:空值检查运算符用于空值(空安全)的主要内容,如果未能解决你的问题,请参考以下文章