You need to enable JavaScript to run this app. Dart空安全环境下assert失败未抛出AssertionError有可行解决办法吗?
优惠活动
大模型
产品
解决方案
定价
更多

Dart空安全环境下assert失败未抛出AssertionError有可行解决办法吗?

迁移到Dart空安全时的AssertionError运行时类型不匹配问题

问题场景

迁移到Dart空安全特性过程中,运行如下测试代码:

//  ignore_for_file: avoid_catching_errors

import 'package:test/test.dart';

void main() {
  test(
      'WHEN `AssertionError` is thrown '
      'THEN `Error` is `AssertionError`'
      '', () {
    Type? type;

    void fun() => throw AssertionError();

    try {
      fun();
    } on Error catch (e) {
      if (e is AssertionError) {
        type = e.runtimeType;
      }
    }

    expect(type, AssertionError);
  });
  test('WHEN `assertion` fails THEN throw `AssertionError`', () {
    Type? type;

    void fun() {
      assert(false);
    }

    try {
      fun();
    } on Error catch (e) {
      if (e is AssertionError) {
        type = e.runtimeType;
      }
    }

    expect(type, AssertionError);
  });

  test('WHEN `assertion fails THEN throwsA(AssertionError) should be true', () {
    bool fun() {
      assert(false);
      return true;
    }

    expect(fun, throwsA(isA<AssertionError>()));
  });
}

运行后输出日志:

00:00 +0: loading test/widget_test.dart                                                                                                                                                                
00:01 +0: loading test/widget_test.dart                                                                                                                                                                
00:01 +0: test/widget_test.dart: WHEN `AssertionError` is thrown THEN `Error` is `AssertionError`                                                                                                                                                                      
00:01 +1: test/widget_test.dart: WHEN `AssertionError` is thrown THEN `Error` is `AssertionError`                                                                                                                                                                      
00:01 +1: test/widget_test.dart: WHEN `assertion` fails THEN throw `AssertionError`                                                                                                                                                                                    
00:01 +1 -1: test/widget_test.dart: WHEN `assertion` fails THEN throw `AssertionError` [E]                                                                                                                                                                               
  Expected: Type:<AssertionError>
    Actual: Type:<_AssertionError>

  package:test_api            expect
  test/widget_test.dart 39:5  main.<fn>


00:01 +1 -1: test/widget_test.dart: WHEN `assertion fails THEN throwsA(AssertionError) should be true                                                                                                                                                                   
00:01 +2 -1: test/widget_test.dart: WHEN `assertion fails THEN throwsA(AssertionError) should be true                                                                                                                                                                   
00:01 +2 -1: Some tests failed.                                                                                                                                                                        
pub failed (1; 00:01 +2 -1: Some tests failed.
)

相关说明

  • 若尝试直接使用_AssertionError进行类型判断,会触发编译错误:

名称"_AssertionError"未定义,因此无法在"is"表达式中使用。请将名称修改为现有类型的名称,或创建名为"_AssertionError"的类型。

  • 该行为并非Bug,而是Dart的预期设计,可参考Dart SDK 45191号issue说明:

不应期望assert(false)抛出的对象运行时类型为AssertionError,官方仅保证抛出的对象是AssertionError的子类型,这也是expect(type, AssertionError);测试用例失败的原因。

  • 测试代码仅为演示用途,不接受"不应捕获错误"这类回答;虽然示例中包含expect(fun(), throwsA(AssertionError));写法,但该写法不适用于实际业务场景。

开发环境信息

[✓] Flutter (Channel master, 2.1.0-11.0.pre.97, on Linux, locale en_US.UTF-8)
    • Flutter version 2.1.0-11.0.pre.97 at /home/francesco/snap/flutter/common/flutter
    • Framework revision c14c8d8177 (2 hours ago), 2021-03-03 21:40:34 -0800
    • Engine revision 72bbc5d92c
    • Dart version 2.13.0 (build 2.13.0-93.0.dev)

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /home/francesco/Android/Sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /home/francesco/Android/Sdk
    • Java binary at: /usr/lib/jvm/java-1.8.0-openjdk-amd64/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_282-8u282-b08-0ubuntu1~20.04-b08)
    • All Android licenses accepted.

[✓] Chrome - develop for the web
    • CHROME_EXECUTABLE = /snap/bin/chromium

[✓] Linux toolchain - develop for Linux desktop
    • clang version 6.0.0-1ubuntu2 (tags/RELEASE_600/final)
    • cmake version 3.10.2
    • ninja version 1.8.2
    • pkg-config version 0.29.1

[!] Android Studio (not installed)
    • Android Studio 未安装,可从官方渠道下载,或参考Flutter官方Linux平台开发配置指南完成环境搭建。

[✓] Connected device (2 available)
    • Linux (desktop) • linux  • linux-x64      • Linux
    • Chrome (web)    • chrome • web-javascript • Chromium 89.0.4389.72 snap

! Doctor found issues in 1 category.

项目pubspec配置

name: issue
description: A new Flutter project.
version: 1.0.0+1

environment:
  sdk: '>=2.12.0 <3.0.0'

dev_dependencies:
  test: ^1.16.5

内容的提问来源于stack exchange,提问作者user9836706

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.01 01:45:05