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

Flutter项目运行报错:AspectRatio源码变量声明异常求助

Flutter项目启动报错:SDK中AspectRatio组件代码异常

问题描述

我的Flutter项目昨日运行完全正常,今日启动时出现异常报错。报错指向Flutter SDK中basic.dart文件内AspectRatio组件的变量声明行(final double aspectRatio;paint),提示变量声明需使用const/final/var或类型名、语句末尾缺少分号等错误。当前我使用Flutter 3.10.0版本,已尝试执行flutter clean和flutter pub get操作,但问题仍未解决。

完整报错日志

Launching lib/main_qc.dart on SM A536E in debug mode...
Running Gradle task 'assembleQcDebug'...
../development/flutter/packages/flutter/lib/src/widgets/basic.dart:3375:28: Error: Variables must be declared using the keywords 'const', 'final', 'var' or a type name.
Try adding the name of the type of the variable or the keyword 'var'.
  final double aspectRatio;paint
                           ^^^^^
../development/flutter/packages/flutter/lib/src/widgets/basic.dart:3375:28: Error: Expected ';' after this.
  final double aspectRatio;paint
                           ^^^^^
../development/flutter/packages/flutter/lib/src/widgets/basic.dart:3365:9: Error: Constructor is marked 'const' so all fields must be final.
  const AspectRatio({
        ^
../development/flutter/packages/flutter/lib/src/widgets/basic.dart:3375:28: Context: Field isn't final, but constructor is 'const'.
  final double aspectRatio;paint
                           ^^^^^
Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

* Where:
Script '/Users/admin/development/flutter/packages/flutter_tools/gradle/src/main/groovy/flutter.groovy' line: 1297

* What went wrong:
Execution failed for task ':app:compileFlutterBuildQcDebug'.
> Process 'command '/Users/admin/development/flutter/bin/flutter'' finished with non-zero exit value 1

* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 52s
Exception: Gradle task assembleQcDebug failed with exit code 1

环境配置

[!] Flutter (Channel stable, 3.13.1, on macOS 13.5.1 22G90 darwin-x64, locale en-VN)
    • Flutter version 3.13.1 on channel stable at /Users/admin/development/flutter
    ! Warning: `dart` on your path resolves to /usr/local/Cellar/dart/3.1.0/libexec/bin/dart, which is not
      inside your current Flutter SDK checkout at /Users/admin/development/flutter. Consider adding
      /Users/admin/development/flutter/bin to the front of your path.
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision e1e47221e8 (5 months ago), 2023-08-22 21:43:18 -0700
    • Engine revision b20183e040
    • Dart version 3.1.0
    • DevTools version 2.25.0
    • If those were intentional, you can disregard the above warnings; however it is recommended to use "git"
      directly to perform update checks and upgrades.

[✓] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/admin/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.6+0-17.0.6b802.4-9586694)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 14.3.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14E300c
    • CocoaPods version 1.12.1

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2022.2)
    • Android Studio at /Applications/Android Studio.app/Contents
    • 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 17.0.6+0-17.0.6b802.4-9586694)

[✓] VS Code (version 1.85.1)
    • VS Code at /Users/admin/development/Visual Studio Code.app/Contents
    • Flutter extension version 3.80.0

[✓] Connected device (4 available)            
    • SM A536E (mobile)      • R5CT40W12LL                          • android-arm64  
[✓] Network resources
    • All expected network resources are available.

解决方案

从报错信息看,basic.dart文件里的代码被意外修改了——正常的AspectRatio组件中不会出现final double aspectRatio;paint这种语法错误的代码。以下是解决步骤:

  • 修复SDK文件
    找到路径/Users/admin/development/flutter/packages/flutter/lib/src/widgets/basic.dart,打开第3375行,删除多余的paint字符,恢复成正常的变量声明:final double aspectRatio;。保存文件后重新编译项目。

  • 重置Flutter SDK
    如果手动修复无效,或者不确定原始代码内容,可以通过Git重置Flutter SDK到干净状态:

    cd /Users/admin/development/flutter
    git reset --hard HEAD
    

    执行完成后,重新运行项目。

  • 检查环境变量
    环境配置中提示系统的dart命令指向了独立安装的版本,而非Flutter SDK自带的Dart。将Flutter的bin目录添加到系统PATH最前面:

    1. 打开终端,编辑~/.zshrc或~/.bash_profile文件
    2. 添加一行:export PATH="/Users/admin/development/flutter/bin:$PATH"
    3. 执行source ~/.zshrc(或对应配置文件)生效,之后重启IDE。
  • 重新安装Flutter SDK
    如果以上方法都无效,可以下载对应版本的Flutter SDK(3.10.0)替换当前的SDK目录,确保下载的SDK包完整无损坏。


内容的提问来源于stack exchange,提问作者N.LanLuu

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.01 19:44:56