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

如何解决Flutter中使用空安全Dart包的SDK版本冲突问题

解决Flutter空安全包依赖冲突问题

问题概述

在Android Studio中使用Flutter开发,想要使用支持空安全的包(如accordion 2.5.1),但所有标注空安全的包都无法正常引入。已将Flutter升级到最新稳定版,更新了Dart包,问题仍未解决。

环境与错误详情

原pubspec.yaml中的SDK配置

sdk: ">=2.7.0 <3.0.5"

修改后的SDK配置

sdk: ">=2.11.0 <3.0.0"

修改后出现的错误

The current Dart SDK version is 2.11.0-213.1.beta.

Because unnamed_cv requires SDK version >=2.11.0 <3.0.5, version solving failed.
pub get failed (1; Because unnamed_cv requires SDK version >=2.11.0 <3.0.5, version solving failed.)

执行flutter packages pub upgrade的错误输出

C:\Users\user\AndroidStudioProjects\unnamed_cv>flutter packages pub upgrade
Resolving dependencies...
The lower bound of "sdk: '>=2.7.0 <3.0.0'" must be 2.12.0'
or higher to enable null safety.

The current Dart SDK (3.0.5) only supports null safety.

flutter doctor -v输出

Flutter (Channel stable, 3.10.5, on Microsoft Windows [Version 10.0.19045.3086], locale tr-TR)
    • Flutter version 3.10.5 on channel stable at C:\Users\user\Documents\flutter_windows_1.22.2-stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 796c8ef792 (7 days ago), 2023-06-13 15:51:02 -0700
    • Engine revision 45f6e00911
    • Dart version 3.0.5
    • DevTools version 2.23.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at C:\Users\user\AppData\Local\Android\sdk
    X cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.

[√] Chrome - develop for the web
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[!] Visual Studio - develop for Windows (Visual Studio Community 2019 16.11.3)
    • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community
    • Visual Studio Community 2019 version 16.11.31702.278
    • Windows 10 SDK version 10.0.19041.0
    X Visual Studio is missing necessary components. Please re-run the Visual Studio installer for the "Desktop development with C++" workload, and include these components:
        MSVC v142 - VS 2019 C++ x64/x86 build tools
         - If there are multiple build tool versions available, install the latest
        C++ CMake tools for Windows
        Windows 10 SDK

[√] Android Studio (version 4.1)
    • Android Studio at C:\Program Files\Android\Android Studio
    • Flutter plugin can be installed from:
       https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
       https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)

[√] VS Code (version 1.78.2)
    • VS Code at C:\Users\user\AppData\Local\Programs\Microsoft VS Code
    • Flutter extension can be installed from:
       https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[√] Connected device (3 available)
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.19045.3086]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 114.0.5735.134
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 114.0.1823.43

[√] Network resources
    • All expected network resources are available.

解决方案

  1. 修正pubspec.yaml中的SDK版本范围
    当前Flutter 3.10.5对应Dart 3.0.5,而Dart 3.x已全面强制空安全,空安全的最低要求是Dart 2.12。将SDK范围修改为:

    sdk: ">=2.12.0 <4.0.0"
    

    该范围既满足空安全要求,也兼容当前Dart版本,同时预留未来升级空间。

  2. 清理缓存并重新获取依赖
    执行以下命令清除旧依赖缓存,避免版本冲突:

    flutter clean
    flutter pub get
    

    若问题仍存在,尝试强制升级所有依赖:

    flutter pub upgrade --major-versions
    
  3. 完成项目空安全迁移
    如果项目是从非空安全版本升级而来,运行迁移命令适配代码:

    dart migrate
    

    按照终端提示完成代码中的空安全标记(如添加?或!)。

  4. 检查Flutter环境路径
    从flutter doctor输出可见,Flutter安装在旧版本目录中,可能存在环境变量冲突。检查系统PATH变量,确保指向最新的Flutter安装目录,或重新安装Flutter到新目录并更新环境变量。

内容的提问来源于stack exchange,提问作者Berkay Isıkoglu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 18:14:56