指定为非空的参数 pendingDynamicLinkData 为空

parameter pendingDynamicLinkData specified as non-null is null

private fun getReferralId() {
    Firebase.dynamicLinks
        .getDynamicLink(intent)
        .addOnSuccessListener(this) { pendingDynamicLinkData ->
            pendingDynamicLinkData?.link?.getQueryParameter(
                DEEP_LINK_QUERY_PARAM_REFERRAL_ID
            )?.let { refId ->
                viewModel.saveReferralId(refId)
            }
        }
}

java.lang.NullPointerException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkNotNullParameter, parameter pendingDynamicLinkData at app.package.activity.MainActivity.getReferralId$lambda-2(Unknown Source:7) at app.package.activity.MainActivity.$r8$lambda$ANLS0uCuXrQe7RFQ5b0C-RFsBKE(Unknown Source:0) at app.package.activity.MainActivity$$ExternalSyntheticLambda3.onSuccess(Unknown Source:4)

版本:

implementation platform("com.google.firebase:firebase-bom:28.3.1")
implementation "com.google.firebase:firebase-dynamic-links-ktx"

这里有什么问题?

更新

当我更新 play-services-auth libs 版本时发生

implementation "com.google.android.gms:play-services-auth:19.2.0"
implementation "com.google.android.gms:play-services-auth-api-phone:17.5.1"

到最新版本

implementation "com.google.android.gms:play-services-auth:20.0.0"
implementation "com.google.android.gms:play-services-auth-api-phone:18.0.0"

这有什么关系?

这是 bug in the library 由于播放服务更新。要修复它,您应该明确声明 pendingDynamicLinkData 可以为 null。

像这样:

private fun getReferralId() {
    Firebase.dynamicLinks
        .getDynamicLink(intent)
        .addOnSuccessListener(this) { pendingDynamicLinkData: PendingDynamicLinkData? ->
            pendingDynamicLinkData?.link?.getQueryParameter(
                DEEP_LINK_QUERY_PARAM_REFERRAL_ID
            )?.let { refId ->
                viewModel.saveReferralId(refId)
            }
        }
 }

@brwngrldev 回答的补充。

更新

该问题已在最新版本的播放服务库 (https://developers.google.com/android/guides/releases#january_05_2022)

中得到解决

检查您的项目中有哪些依赖项,并将它们更新为“maven.google.com 上发布的工件”中列出的版本。

例如问题的issue,需要更新以下依赖:

implementation "com.google.android.gms:play-services-auth:20.0.1"
implementation "com.google.android.gms:play-services-auth-api-phone:18.0.1"

旧答案

该错误已在 play-services-base 版本 (https://developers.google.com/android/guides/releases#december_16_2021) 中修复

如果您不希望将该字段显式设置为可为空,则可以通过将这两行添加到您应用的 build.gradle.

来更新播放服务库
implementation 'com.google.android.gms:play-services-base:18.0.1'
implementation 'com.google.android.gms:play-services-tasks:18.0.1'

学分:https://github.com/firebase/firebase-android-sdk/issues/2992#issuecomment-1001847825