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

Flutter 3.22.0升级后运行失败,求解决方案

Flutter 3.22.0升级后运行失败问题解决指南

问题概述

将Flutter升级至3.22.0后项目无法运行,已升级Gradle和Java 17,执行flutter clean && flutter pub cache clean && flutter pub get无效,核心疑问:

  • Flutter 3.22.0适配的Gradle版本是多少?
  • 当前报错是否为插件兼容问题?

报错信息

2024-08-07T17:19:56.508+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/pdfx-2.4.0/lib/src/viewer/pdf_page_image_provider.dart:24:55: Error: Type 'DecoderCallback' not found.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]   ImageStreamCompleter load(PdfPageImageProvider key, DecoderCallback decode) =>
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]                                                       ^^^^^^^^^^^^^^^
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/pdfx-2.4.0/lib/src/viewer/pdf_page_image_provider.dart:40:7: Error: Type 'DecoderCallback' not found.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]       DecoderCallback decode) async {
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]       ^^^^^^^^^^^^^^^
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/pdfx-2.4.0/lib/src/viewer/pdf_page_image_provider.dart:24:55: Error: 'DecoderCallback' isn't a type.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]   ImageStreamCompleter load(PdfPageImageProvider key, DecoderCallback decode) =>
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]                                                       ^^^^^^^^^^^^^^^
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/pdfx-2.4.0/lib/src/viewer/pdf_page_image_provider.dart:40:7: Error: 'DecoderCallback' isn't a type.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]       DecoderCallback decode) async {
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]       ^^^^^^^^^^^^^^^
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/gif-2.2.0/lib/gif.dart:348:51: Error: The method 'instantiateImageCodec' isn't defined for the class 'PaintingBinding'.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]  - 'PaintingBinding' is from 'package:flutter/src/painting/binding.dart' ('../../../fvm/versions/3.16.0/packages/flutter/lib/src/painting/binding.dart').
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] Try correcting the name to the name of an existing method, or defining a method named 'instantiateImageCodec'.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]     Codec codec = await PaintingBinding.instance!.instantiateImageCodec(bytes);
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]                                                   ^^^^^^^^^^^^^^^^^^^^^
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] ../../../.pub-cache/hosted/pub.dev/gradient_widgets-0.6.0/lib/src/gradient_widget.dart:104:73: Error: The getter 'button' isn't defined for the class 'TextTheme'.
2024-08-07T17:19:56.511+0800 [ERROR] [system.err]  - 'TextTheme' is from 'package:flutter/src/material/text_theme.dart' ('../../../fvm/versions/3.16.0/packages/flutter/lib/src/material/text_theme.dart').
2024-08-07T17:19:56.511+0800 [ERROR] [system.err] Try correcting the name to the name of an existing getter, or defining a getter or field named 'button'.
2024-08-07T17:19:56.512+0800 [ERROR] [system.err]     final TextStyle textStyleCopy = widget.textStyle ?? theme.textTheme.button!.copyWith(color: Colors.white);
2024-08-07T17:19:56.512+0800 [ERROR] [system.err]                                                                         ^^^^^^
2024-08-07T17:19:56.784+0800 [ERROR] [system.err] Target kernel_snapshot_program failed: Exception
2024-08-07T17:19:56.820+0800 [ERROR] [system.err] 
2024-08-07T17:19:56.911+0800 [DEBUG] [org.gradle.process.internal.DefaultExecHandle] Changing state to: FAILED

项目配置文件

settings.gradle

pluginManagement {
    def flutterSdkPath = {
        def properties = new Properties()
        file("local.properties").withInputStream { properties.load(it) }
        def flutterSdkPath = properties.getProperty("flutter.sdk")
        assert flutterSdkPath != null, "flutter.sdk not set in local.properties"
        return flutterSdkPath
    }
    settings.ext.flutterSdkPath = flutterSdkPath()

    includeBuild("${settings.ext.flutterSdkPath}/packages/flutter_tools/gradle")

    repositories {
        google()
        mavenCentral()
        gradlePluginPortal()
    }
}
plugins {
    id "dev.flutter.flutter-plugin-loader"
    id "com.android.application" version '8.5.1' apply false
    id 'com.android.library' version '7.1.2' apply false
    id "org.jetbrains.kotlin.android" version "1.8.10" apply false
}

include ':app'

gradle-wrapper.properties

distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists

build.gradle (Module)

def localProperties = new Properties()
def localPropertiesFile = rootProject.file('local.properties')
if (localPropertiesFile.exists()) {
    localPropertiesFile.withReader('UTF-8') { reader ->
        localProperties.load(reader)
    }
}

def flutterRoot = localProperties.getProperty('flutter.sdk')
if (flutterRoot == null) {
    throw new GradleException("Flutter SDK not found. Define location with flutter.sdk in the local.properties file.")
}

def flutterVersionCode = localProperties.getProperty('flutter.versionCode')
if (flutterVersionCode == null) {
    flutterVersionCode = '1'
}

def flutterVersionName = localProperties.getProperty('flutter.versionName')
if (flutterVersionName == null) {
    flutterVersionName = '1.0'
}

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"
//apply plugin: 'com.google.gms.google-services'


def keystoreProperties = new Properties()
def keystorePropertiesFile = rootProject.file('key.properties')
if (keystorePropertiesFile.exists()) {
    keystoreProperties.load(new FileInputStream(keystorePropertiesFile))
}

android {
    compileSdkVersion 34

    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }


    kotlinOptions {
        jvmTarget = "17"
    }

    sourceSets {
        main.java.srcDirs += 'src/main/kotlin'
    }

    defaultConfig {
        // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
        applicationId "com.example"
        minSdkVersion 23
        targetSdkVersion 34
        versionCode flutterVersionCode.toInteger()
        versionName flutterVersionName

        ndk {
            abiFilters 'arm64-v8a', 'armeabi-v7a', 'x86_64'
        }

        multiDexEnabled true
    }

    signingConfigs {
        release {
            keyAlias keystoreProperties['keyAlias']
            keyPassword keystoreProperties['keyPassword']
            storeFile keystoreProperties['storeFile'] ? file(keystoreProperties['storeFile']) : null
            storePassword keystoreProperties['storePassword']
        }
    }

    buildTypes {
        release {
            signingConfig signingConfigs.release
            
            minifyEnabled false
            shrinkResources false

        }
    }
 
}

android {
    namespace 'com.example'
}

flutter {
    source '../..'
}


dependencies {
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
    implementation platform('com.google.firebase:firebase-bom:29.3.1')
    implementation 'com.google.firebase:firebase-analytics-ktx'
    implementation 'com.google.firebase:firebase-dynamic-links-ktx'
    implementation 'com.squareup.retrofit2:retrofit:2.2.0'
    implementation 'com.squareup.retrofit2:converter-gson:2.2.0'
    implementation 'com.facebook.android:facebook-android-sdk:[8,9)'
    implementation fileTree(include: ['*.jar', '*.arr'], dir: 'libs')

    api fileTree(dir: 'libs', include: ['*.jar','*.aar'])

    implementation("androidx.recyclerview:recyclerview:1.2.1")
    implementation("androidx.recyclerview:recyclerview-selection:1.1.0")
    implementation("androidx.cardview:cardview:1.0.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    implementation("androidx.constraintlayout:constraintlayout-compose:1.0.1")

    implementation 'com.github.bumptech.glide:glide:4.7.1'

    implementation("androidx.multidex:multidex:2.0.1")

    implementation 'com.github.getActivity:MultiLanguages:8.0'
    implementation "com.github.kongzue.DialogX:DialogX:0.0.46"

}

build.gradle (Project)

buildscript {
    ext.kotlin_version = '1.9.23'
    repositories {
        google()
        mavenCentral()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:8.5.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
        classpath 'com.google.gms:google-services:4.4.0'
    }
}

allprojects {
    repositories {
        google()
        mavenCentral()
    }
}

rootProject.buildDir = '../build'
subprojects {
    project.buildDir = "${rootProject.buildDir}/${project.name}"
}
subprojects {
    project.evaluationDependsOn(':app')
}

tasks.register("clean", Delete) {
    delete rootProject.buildDir
}

问题分析与解决方案

1. Flutter 3.22.0适配的Gradle版本

Flutter 3.22.0官方适配的Gradle版本为8.7,Android Gradle Plugin (AGP)版本为8.5.0+。你的当前配置中gradle-wrapper.properties已经使用了gradle-8.7,AGP版本为8.5.1,符合要求,所以Gradle配置没问题。

2. 核心问题:第三方插件兼容性

从报错信息来看,完全是旧版第三方插件与Flutter 3.22.0的API变更不兼容导致:

  • pdfx-2.4.0:Flutter 3.10+中DecoderCallback已被移除或重命名,旧版插件仍在使用该类型
  • gif-2.2.0:PaintingBinding.instantiateImageCodec方法已被迁移到ImageCache或其他API,旧版插件调用路径错误
  • gradient_widgets-0.6.0:Flutter 3.16+中TextTheme.button已被移除,替换为TextTheme.labelLarge等新样式

解决方案

  • 更新所有报错的第三方插件到最新版本:
    • 打开pubspec.yaml,将pdfx升级到最新版(当前最新版已适配Flutter 3.22+)
    • 将gif插件替换为维护更活跃的替代(如flutter_gif)或升级到最新兼容版本
    • 将gradient_widgets升级到最新版,或手动修改插件代码中的textTheme.button为textTheme.labelLarge
  • 执行以下命令重新同步依赖:
    flutter clean
    flutter pub upgrade
    flutter pub get
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.19 21:43:10