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

Windows更新后Flutter应用编译失败:AtomicResult错误及依赖冲突

解决Flutter编译失败与Amplify依赖版本冲突问题

一、编译错误详情

更新Windows系统后,Flutter应用编译失败,错误指向amplify_core依赖中的AtomicResult.kt文件,具体日志如下:

e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (87, 2): Missing '}'
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (29, 9): Class 'AtomicResult' is not abstract and does not implement abstract member public abstract fun success(p0: Any?): Unit defined in io.flutter.plugin.common.MethodChannel.Result
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (46, 5): 'success' overrides nothing
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (47, 9): Unresolved reference: scope
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (50, 37): Unresolved reference: operation
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (55, 13): Unresolved reference: result
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (59, 5): 'error' overrides nothing
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (60, 9): Unresolved reference: scope
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (63, 37): Unresolved reference: operation
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (71, 13): Unresolved reference: result
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (75, 5): 'notImplemented' overrides nothing
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (76, 9): Unresolved reference: scope
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (79, 37): Unresolved reference: operation
e: C:\Users\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt: (84, 13): Unresolved reference: result

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':amplify_core:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction
   > Compilation error. See log for more details

* 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 5s
Exception: Gradle task assembleDebug failed with exit code 1

二、问题文件AtomicResult.kt内容

该文件存在明显语法错误,具体代码如下:

package com.amazonaws.amplify.amplify_core

import io.flutter.Log
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.*
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Thread-safe [MethodChannel.Result] wrapper which prevents multiple replies and automatically posts
 * results to the main thread.
 */
class AtomicResult(private val result: MethodChannel.Result, private val operation: String?) :
    MethodChannel.Result {
    private companion object {
        class AtomicResult(private val result: MethodChannel.Result, private val operation: String?) :
                MethodChannel.Result {
            private companion object {
                /**
                 * Scope for performing result handling.
                 * Method channel results must be sent on the main (UI) thread.
                 */
                val scope = MainScope()
            }
        val scope = MainScope()
    }

    /**
     * Whether a response has been sent.
     */
    private val isSent = AtomicBoolean(false)

    override fun success(value: Any?) {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    "Attempted to send success value after initial reply"
                )
                return@launch
            }
            result.success(value)
        }
    }

    override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    """
                    Attempted to send error value after initial reply:
                    | PlatformException{code=${errorCode}, message=${errorMessage}, details=${errorDetails}}
                    """.trimMargin()
                )
                return@launch
            }
            result.error(errorCode, errorMessage, errorDetails)
        }
    }

    override fun notImplemented() {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    "Attempted to send notImplemented value after initial reply"
                )
                return@launch
            }
            result.notImplemented()
        }
    }
}

三、版本冲突错误信息

尝试升级amplify_core版本时,出现依赖版本不兼容问题,日志如下:

Because no versions of amplify_auth_cognito match >0.2.10 <0.3.0 and amplify_auth_cognito >=0.2.0 <0.2.1 depends on amplify_core 0.2.0, amplify_auth_cognito >=0.2.0 <0.2.1-∞ or >0.2.10 <0.3.0 requires amplify_core 0.2.0.
And because amplify_auth_cognito >=0.2.1 <0.2.2 depends on amplify_core 0.2.1 and amplify_auth_cognito >=0.2.2 <0.2.3 depends on amplify_core 0.2.2, amplify_auth_cognito >=0.2.0 <0.2.3-∞ or >0.2.10 <0.3.0 requires amplify_core 0.2.0 or 0.2.1 or 0.2.2.
And because amplify_auth_cognito >=0.2.3 <0.2.4 depends on amplify_core 0.2.3 and amplify_auth_cognito >=0.2.4 <0.2.5 depends on amplify_core 0.2.4, amplify_auth_cognito >=0.2.0 <0.2.5-∞ or >0.2.10 <0.3.0 requires amplify_core 0.2.0 or 0.2.1 or 0.2.2 or 0.2.3 or 0.2.4.
And because amplify_auth_cognito >=0.2.5 <0.2.6 depends on amplify_core 0.2.5 and amplify_auth_cognito >=0.2.6 <0.2.7 depends on amplify_core 0.2.6, amplify_auth_cognito >=0.2.0 <0.2.7-∞ or >0.2.10 <0.3.0 requires amplify_core 0.2.0 or 0.2.1 or 0.2.2 or 0.2.3 or 0.2.4 or 0.2.5 or 0.2.6.
And because amplify_auth_cognito >=0.2.7 <0.2.8 depends on amplify_core 0.2.7 and amplify_auth_cognito >=0.2.8 <0.2.9 depends on amplify_core 0.2.8, amplify_auth_cognito >=0.2.0 <0.2.9-∞ or >0.2.10 <0.3.0 requires amplify_core 0.2.0 or 0.2.1 or 0.2.2 or 0.2.3 or 0.2.4 or 0.2.5 or 0.2.6 or 0.2.7 or 0.2.8.
And because amplify_auth_cognito >=0.2.9 <0.2.10 depends on amplify_core 0.2.9 and amplify_auth_cognito 0.2.10 depends on amplify_core 0.2.10, amplify_auth_cognito ^0.2.0 requires amplify_core 0.2.0 or 0.2.1 or 0.2.2 or 0.2.3 or 0.2.4 or 0.2.5 or 0.2.6 or 0.2.7 or 0.2.8 or 0.2.9 or 0.2.10.
So, because newday depends on both amplify_auth_cognito ^0.2.0 and amplify_core ^0.6.6, version solving failed.
pub get failed (1; So, because newday depends on both amplify_auth_cognito ^0.2.0 and amplify_core ^0.6.6, version solving failed.)

四、项目依赖配置

当前pubspec.yaml中的依赖配置:

dependencies:
  flutter:
    sdk: flutter
  rxdart: ^0.27.5

  provider: ^6.0.3
  amplify_flutter: <1.0.0  #1.0.0
  amplify_auth_cognito: ^0.2.0 #0.2.0
  hooks_riverpod: ^1.0.3
  flutter_hooks: ^0.18.2
  riverpod: ^1.0.3
  curved_navigation_bar: ^1.0.3
  just_audio:
  amplify_core: ^0.6.6
  video_player: ^2.3.0
  page_transition: ^2.0.5
  flash_card: ^0.0.7
  community_material_icon: ^5.9.55
  flutter_swipable: ^1.2.1

解决方案

方案1:临时修复缓存文件语法错误

AtomicResult.kt的核心问题是类定义重复嵌套,导致外部类无法访问scope、operation等变量。直接修改缓存中的文件:

  1. 打开路径:C:\Users\<你的用户名>\AppData\Local\Pub\Cache\hosted\pub.dartlang.org\amplify_core-0.2.10\android\src\main\kotlin\com\amazonaws\amplify\amplify_core\AtomicResult.kt
  2. 删除内部嵌套的AtomicResult类定义,替换为以下正确代码:
package com.amazonaws.amplify.amplify_core

import io.flutter.Log
import io.flutter.plugin.common.MethodChannel
import kotlinx.coroutines.*
import java.util.concurrent.atomic.AtomicBoolean

/**
 * Thread-safe [MethodChannel.Result] wrapper which prevents multiple replies and automatically posts
 * results to the main thread.
 */
class AtomicResult(private val result: MethodChannel.Result, private val operation: String?) :
    MethodChannel.Result {
    private companion object {
        /**
         * Scope for performing result handling.
         * Method channel results must be sent on the main (UI) thread.
         */
        val scope = MainScope()
    }

    /**
     * Whether a response has been sent.
     */
    private val isSent = AtomicBoolean(false)

    override fun success(value: Any?) {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    "Attempted to send success value after initial reply"
                )
                return@launch
            }
            result.success(value)
        }
    }

    override fun error(errorCode: String?, errorMessage: String?, errorDetails: Any?) {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    """
                    Attempted to send error value after initial reply:
                    | PlatformException{code=${errorCode}, message=${errorMessage}, details=${errorDetails}}
                    """.trimMargin()
                )
                return@launch
            }
            result.error(errorCode, errorMessage, errorDetails)
        }
    }

    override fun notImplemented() {
        scope.launch {
            if (isSent.getAndSet(true)) {
                Log.w(
                    "AtomicResult(${operation})",
                    "Attempted to send notImplemented value after initial reply"
                )
                return@launch
            }
            result.notImplemented()
        }
    }
}
  1. 重新运行flutter run即可完成编译。

方案2:统一Amplify依赖版本(彻底解决)

版本冲突的根源是amplify_auth_cognito ^0.2.0仅兼容amplify_core 0.2.x,但你手动指定了更高版本的amplify_core。建议升级到稳定的1.x版本,统一所有Amplify依赖:

  1. 修改pubspec.yaml中的Amplify相关依赖:
dependencies:
  flutter:
    sdk: flutter
  rxdart: ^0.27.5

  provider: ^6.0.3
  amplify_flutter: ^1.0.0
  amplify_auth_cognito: ^1.0.0
  hooks_riverpod: ^1.0.3
  flutter_hooks: ^0.18.2
  riverpod: ^1.0.3
  curved_navigation_bar: ^1.0.3
  just_audio:
  video_player: ^2.3.0
  page_transition: ^2.0.5
  flash_card: ^0.0.7
  community_material_icon: ^5.9.55
  flutter_swipable: ^1.2.1

注:移除了单独的amplify_core依赖,因为amplify_flutter已包含该包

  1. 执行命令更新依赖:
flutter clean
flutter pub get
  1. 若升级后出现API变更,可根据Amplify官方文档调整代码(1.x版本为稳定版,核心API与0.x兼容度较高)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.21 16:54:18