You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter环境突发故障:运行模板应用出现空安全及类型匹配错误

问题描述

这是典型的“昨日正常运行今日报错”场景:Flutter环境原本配置完成且运行正常,推测因Dart自动更新后,今日运行模板应用时出现编译错误,报错信息如下:

../../AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.5.0/lib/palettes/tonal_palette.dart:110:42: Error: Inferred type argument 'double?' doesn't conform to the bound 'num' of the type variable 'T' on 'min'.
Try specifying type arguments explicitly so that they conform to the bounds.
    final chroma = (tone >= 90.0) ? math.min(_chroma, 40.0) : _chroma;
                                         ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.5.0/lib/palettes/tonal_palette.dart:112:30: Error: The argument type 'double?' can't be assigned to the parameter type 'double' because 'double?' is nullable and 'double' isn't.
        tone, () => Hct.from(_hue, chroma, tone.toDouble()).toInt());
                             ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/material_color_utilities-0.5.0/lib/palettes/tonal_palette.dart:112:36: Error: The argument type 'double?' can't be assigned to the parameter type 'double' because 'double?' is nullable and 'double' isn't.
        tone, () => Hct.from(_hue, chroma, tone.toDouble()).toInt());
                                   ^
../../AppData/Local/Pub/Cache/hosted/pub.dev/collection-1.18.0/lib/src/canonicalized_map.dart:173:60: Error: Can't use an expression of type 'bool Function(K)?' as a function because it's potentially null.
Try calling using ?.call instead.
      (key is K) && (_isValidKeyFn == null || _isValidKeyFn(key));
                                                           ^
Failed to compile application.

已尝试flutter doctor和dart fix均未解决;此前导入charts.dart后,dart fix修复了其unnecessary_non_null_assertion问题,但未解决当前故障。

运行环境:

  • Chrome
  • Flutter 3.16.6
  • Dart 3.2.3
解决方案

这类错误源于依赖包版本与当前Dart/Flutter版本不兼容,可按以下步骤解决:

  • 更新兼容的依赖包版本
    在项目根目录的pubspec.yaml中,指定适配Dart 3.2+的依赖版本:

    dependencies:
      # 保留其他原有依赖
      material_color_utilities: ^0.8.0
      collection: ^1.18.1
    

    执行命令更新依赖:

    flutter pub get
    
  • 清理缓存并重建项目
    若更新依赖后仍报错,清理pub缓存并重新构建:

    flutter pub cache clean
    flutter pub get
    flutter clean
    flutter run
    
  • 锁定Dart/Flutter版本(可选)
    若需避免自动更新带来的兼容问题,切换到Flutter稳定分支并锁定版本:

    flutter channel stable
    flutter upgrade --force
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 00:40:30