【问题标题】:Flutter integration test raising an error "null check operator used on a null value"Flutter 集成测试引发错误“null check operator used on a null value”
【发布时间】:2021-12-01 21:45:42
【问题描述】:
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
import 'package:integration_test/integration_test.dart';
import 'package:knights_bridge/main.dart' as app;
import 'dart:io';
import 'package:knights_bridge/screens/shared/bigButtonFilled.dart';

void main() {
  group('Sign in test', () {
    IntegrationTestWidgetsFlutterBinding.ensureInitialized();
    testWidgets('Validate sign in and dashboard', (tester) async {
      app.main();

      await tester.pumpAndSettle();
      final emailField = find.byKey(Key('login'));
      final passwordField = find.byKey(Key('password'));
      final signInButton = find.text('Sign in');
      // final signInButton = find.byType(BigFilledButton);

      print("Starting typing in email field");
      await tester.enterText(emailField, "ashiq@gmail.com");

      print("Starting typing in password field");
      await tester.enterText(passwordField, "123456789As@");

      await tester.pumpAndSettle();
      print("Clicking on sign in button");
      await tester.tap(signInButton);

      await tester.pumpAndSettle();
      final signInMessage = find.text("Login successful");

      print("Started verifying the message for successful login.");
      await tester.ensureVisible(signInMessage);
      await tester.pumpAndSettle(Duration(seconds: 4));

      print("Successfully the success message in dashboard.");
    });
  });
}

这是错误截图:

当我执行此代码时,它正在运行自动化但给出错误并且测试失败。 当我手动运行这个应用程序时没有出现这样的错误,只有在执行集成测试时才会出现。

请检查并告诉我有什么办法可以解决这个问题。

提前致谢。

【问题讨论】:

  • 请添加您正在测试的源代码,因为这将是您对空值使用空检查运算符的地方。
  • 很遗憾我不能在这里分享源代码,因为它是一个保密协议站点。给我一些关于我应该在哪里寻找问题的建议。手动运行应用程序没有问题,没有错误消息。
  • 这确实使询问 SO 非常困难,所以我想看看您是否可以向您的雇主/代码所有者询问您将来可以发布多少。我会尽量在下面的答案中给出我最好的答案

标签: flutter flutter-integration-test


【解决方案1】:

看起来问题来自您的登录页面第 38 行(通过查看您的错误堆栈跟踪)

该错误是由您对 null 的值使用空检查运算符 ! 引起的。您应该先尝试检查空值,而不是使用!。 例如

if(possiblyNullValue == null){
  do something
} else {
  do the thing you wanted with possiblyNullValue
}

在这种情况下,编译器将为您提供帮助,因为如果您检查可能的 null,您的代码将不再需要 ! 运算符。 (应该在您的! 下方显示一条黄线,告诉您不再需要它)

【讨论】:

    猜你喜欢
    • 2022-10-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-07-25
    • 2013-03-07
    相关资源
    最近更新 更多