flutter在windows平台中运行报错

复制代码
PS D:\F\luichun> flutter run

当运行flutter项目时,【解决如下报错】

复制代码
/C:/flutter/packages/flutter/lib/src/painting/star_border.dart:530:27: Error: The getter 'Matrix4' isn't defined for the class '_StarGenerator'.
 - '_StarGenerator' is from 'package:flutter/src/painting/star_border.dart' ('/C:/flutter/packages/flutter/lib/src/painting/star_border.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Matrix4'.
    squashMatrix.multiply(Matrix4.translationValues(-rect.center.dx, -rect.center.dy, 0));
                          ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:643:9: Error: 'Matrix4' isn't a type.
  final Matrix4? transform;
        ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:1756:3: Error: 'Matrix4' isn't a type.
  Matrix4? _transform;
  ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:1757:17: Error: 'Matrix4' isn't a type.
  set transform(Matrix4? value) {
                ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:2745:12: Error: Undefined name 'Matrix4'.
    return Matrix4.identity().storage;
           ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:3213:9: Error: 'Vector3' isn't a type.
  final Vector3 vector = Vector3(point.dx, point.dy, 0.0);
        ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:3213:26: Error: Method not found: 'Vector3'.
  final Vector3 vector = Vector3(point.dx, point.dy, 0.0);
                         ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:3510:13: Error: 'Matrix4' isn't a type.
      final Matrix4 inverse = Matrix4.identity();
            ^^^^^^^
/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart:3510:31: Error: The getter 'Matrix4' isn't defined for the class 'SemanticsOwner'.
 - 'SemanticsOwner' is from 'package:flutter/src/semantics/semantics.dart' ('/C:/flutter/packages/flutter/lib/src/semantics/semantics.dart').
Try correcting the name to the name of an existing getter, or defining a getter or field named 'Matrix4'.
      final Matrix4 inverse = Matrix4.identity();
                              ^^^^^^^
Target kernel_snapshot failed: Exception


FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\flutter\bin\flutter.bat'' 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 8s
Running Gradle task 'assembleDebug'...                              9.3s
Error: Gradle task assembleDebug failed with exit code 1
PS D:\F\luichun> 

查看本地版本

复制代码
PS D:\F\luichun> flutter --version
Flutter 3.19.4 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 68bfaea224 (10 months ago) • 2024-03-20 15:36:31 -0700
Engine • revision a5c24f538d
Tools • Dart 3.3.2 • DevTools 2.31.1
PS D:\F\luichun> 
PS D:\F\luichun> flutter channel stable
Flutter assets will be downloaded from https://storage.googleapis.com. Make sure you trust this source!
Switching to flutter channel 'stable'...

Upgrading engine...
Flutter assets will be downloaded from https://storage.googleapis.com. Make sure you trust this source!
Successfully switched to flutter channel 'stable'.
To ensure that you're on the latest build from this channel, run 'flutter upgrade'
PS D:\F\luichun> 

使用本地版本

复制代码
# 切换到 stable 渠道
flutter channel stable

升级flutter

复制代码
PS D:\F\luichun> flutter upgrade

[√] Chrome - develop for the web
[√] Visual Studio - develop Windows apps (Visual Studio Community 2022 17.9.5)
[√] Android Studio (version 2023.2)
[√] VS Code (version 1.96.2)
[√] Connected device (4 available)
[√] Network resources

! Doctor found issues in 1 category.
PS D:\F\luichun>

清理缓存

复制代码
# 1. 清理项目
PS D:\F\luichun> flutter clean

# 2. 获取新依赖
PS D:\F\luichun> flutter pub get

# 3. 运行项目
PS D:\F\luichun> flutter run

开启本地windows的开发者选项

复制代码
   start ms-settings:developers

运行项目

复制代码
flutter run

前提:你可以上google

如你再次运行时遇到

复制代码
WARNING: [Processor] Library 'C:\Users\luich\.gradle\caches\modules-2\files-2.1\androidx.media2\media2-session\1.2.1\f927563711e36371c19b2e86fb7ccf1c3f259ca4\media2-session-1.2.1.aar' contains references to both AndroidX and old support library. This seems like the library is partially migrated. Jetifier will try to rewrite the library anyway.
 Example of androidX reference: 'androidx/media2/session/MediaBrowser$Builder'
 Example of support library reference: 'android/support/v4/media/session/MediaSessionCompat$Token'

这个警告是关于 media2-session 库中同时存在 AndroidX 和旧版 support library 的引用。这通常与音频相关的功能有关。让我们解决这个问题:

首先检查你的项目是否真的需要 media2-session。在 pubspec.yaml 中查找相关依赖:

复制代码
dependencies:
  vector_math: ^2.1.4
  win32: ^5.10.0
  # 检查是否有音频相关的依赖

在 android/app/build.gradle 中确保正确配置 AndroidX:

复制代码
# 添加
dependencies {
    implementation "androidx.media2:media2-session:1.2.1"
}
# 添加
    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.luichun.luichun"
        // You can update the following values to match your application needs.
        // For more information, see: https://docs.flutter.dev/deployment/android#reviewing-the-gradle-build-configuration.
        minSdkVersion flutter.minSdkVersion
        targetSdkVersion flutter.targetSdkVersion
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName
        # 添加
        android.useAndroidX=true
        # 添加
        android.enableJetifier=true
    }
相关推荐
天天开发1 小时前
Flutter每日库: local_auth本地设备验证插件
flutter
天天开发2 小时前
Flutter每日库: logger自定义日志格式并输出到文件
flutter
美摄科技3 小时前
为什么选择Flutter美颜SDK?
flutter
程序员老刘19 小时前
跨平台开发地图:客户端技术选型指南 | 2025年11月 |(Valdi 加入战场)
flutter·react native·客户端
西西学代码20 小时前
Flutter---Listview横向滚动列表(2)
linux·运维·flutter
未来猫咪花21 小时前
🔥 神奇的 Dart Zone 机制
flutter
AskHarries1 天前
RevenueCat 接入 Apple App Store 订阅全流程详解(2025 最新)
flutter·ios·app
白茶三许1 天前
关于Flutter版本过低导致鸿蒙虚拟机启动失败的问题解决
flutter·开源·harmonyos·openharmony
消失的旧时光-19432 天前
Flutter 与 React/Vue 为什么思想一致?——声明式 UI 体系的深度对比(超清晰版)
vue.js·flutter·react.js
rainboy2 天前
Flutter :自己动手,封装一个小巧精致的气泡弹窗库
前端·flutter·github