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

Flutter升级至3.3后MainActivity.kt空安全编译错误修复及文件疑问

Flutter升级至3.x版本后Kotlin空安全编译错误

编译错误信息

e: path/MainActivity.kt: (82, 34): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List?
e: path/MainActivity.kt: (83, 37): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List?
e: path/MainActivity.kt: (84, 37): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List?
e: path/MainActivity.kt: (85, 38): Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type List?

构建失败,任务:app:compileDebugKotlin执行失败,提示编译错误。

相关代码片段

Android端(MainActivity.kt)

MethodChannel(flutterEngine.dartExecutor.binaryMessenger, LUCA_CHANNEL).setMethodCallHandler{
            call, result ->
            when (call.method) {
                "isLucaQRCode" ->
                    val qrCode = call.argument<String>("qrCode")
                    if(qrCode != null) {
                        result.success(lucaEvaluator.isLucaQRCode(qrCode))
                    }
                "processLucaQRCode" ->
                    val qrCode = call.arguments<List<String>>()[0];
                    val scannerId = call.arguments<List<String>>()[1];
                    val publicKey = call.arguments<List<String>>()[2];
                    val locationId = call.arguments<List<String>>()[3];
                    if(qrCode != null && scannerId != null && publicKey != null && locationId != null) {
                        result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode,scannerId,publicKey,locationId))
                    }
            }
        }
    }

Flutter端对应方法

Future<LucaEvaluationResult?> processLucaQRCode(String? qrCode, String? eventId) async {
    final lucaConfiguration = _getLucaConfiguration(eventId);
    if(lucaConfiguration == null || qrCode == null) {
      return null;
    }
    try {
      final res = await methodChannel.invokeMethod('processLucaQRCode', [
        qrCode,
        lucaConfiguration.lucaScannerId,
        lucaConfiguration.lucaLocationPublicKey,
        lucaConfiguration.lucaLocationId,
      ]);
      if(res != null) {
        return LucaEvaluationResult.fromJson(json.decode(res));
      }
    } on Exception catch (e) {
      //log(e.toString());
    }
    return null;
  }

修复方案

关于MainActivity.kt的说明

该文件并非自动生成(除非是Flutter初始化默认文件,但此处已自定义MethodChannel逻辑),可直接手动编辑,路径为android/app/src/main/kotlin/[你的包名]/MainActivity.kt。

错误原因

Flutter 3.x版本对Kotlin端的空安全检查更严格,call.arguments<List<String>>()返回的是可空类型List<String>?,不能直接通过[]运算符访问元素,必须先处理空值。

修复后的代码

修改processLucaQRCode分支的逻辑,先校验参数列表的非空性和长度,再安全取值:

"processLucaQRCode" ->
    // 先获取参数列表并判断非空
    val args = call.arguments<List<String>>()
    if (args != null && args.size >= 4) {
        val qrCode = args[0]
        val scannerId = args[1]
        val publicKey = args[2]
        val locationId = args[3]
        // 校验每个参数非空
        if (qrCode != null && scannerId != null && publicKey != null && locationId != null) {
            result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode, scannerId, publicKey, locationId))
        } else {
            // 参数为空时返回错误
            result.error("NULL_ARGUMENT", "One or more required arguments are null", null)
        }
    } else {
        // 参数列表为空或长度不足时返回错误
        result.error("INVALID_ARGUMENTS", "Expected a list with 4 non-null String arguments", null)
    }

也可以用Kotlin的安全调用+let简化代码:

"processLucaQRCode" ->
    call.arguments<List<String>>()?.let { args ->
        if (args.size >= 4) {
            val qrCode = args[0]
            val scannerId = args[1]
            val publicKey = args[2]
            val locationId = args[3]
            if (qrCode != null && scannerId != null && publicKey != null && locationId != null) {
                result.success(lucaEvaluator.processLucaQRCodeToStringOutput(qrCode, scannerId, publicKey, locationId))
            } else {
                result.error("NULL_ARGUMENT", "One or more required arguments are null", null)
            }
        } else {
            result.error("INVALID_ARGUMENT_COUNT", "Need exactly 4 arguments", null)
        }
    } ?: run {
        result.error("NULL_ARGUMENTS", "Arguments list cannot be null", null)
    }

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.18 07:30:24