firebase-android-sdk: [Bug] [Dynamic Links] Dynamic Links callback in Kotlin incorrectly marked as non-null

[READ] Step 1: Are you in the right place?

Issues filed here should be about bugs in the code in this repository. If you have a general question, need help debugging, or fall into some other category use one of these other channels:

  • For general technical questions, post a question on StackOverflow with the firebase tag.
  • For general Firebase discussion, use the firebase-talk google group.
  • For help troubleshooting your application that does not fall under one of the above categories, reach out to the personalized Firebase support channel.

[REQUIRED] Step 2: Describe your environment

  • Android Studio version: Arctic Fox 2020.3.1 patch 2
  • Firebase Component: Dynamic Links KTX
  • Component version: 20.1.1

[REQUIRED] Step 3: Describe the problem

When running the sample code for Dynamic Links in Kotlin, the result of the addOnSuccessListener is not correctly marked as a nullable type, despite it being possible to return as null.

Steps to reproduce:

  • Follow the sample code setup in Kotlin
  • Call the sample code with an intent that doesn’t return pendingDynamicLinkData
  • Observe that null is returned for the non-nullable type param of the success callback

Relevant Code:

Firebase.dynamicLinks
        .getDynamicLink(intent)
        .addOnSuccessListener(this) { pendingDynamicLinkData ->

            // **BUG** Android Studio reports this check to always be true
            //  as the typing of pendingDynamicLinkData is not nullable
            if (pendingDynamicLinkData != null) {
               // some code here ...
            }
        }
        .addOnFailureListener(this) { e -> Log.w(TAG, "getDynamicLink:onFailure", e) }

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 22 (8 by maintainers)

Most upvoted comments

Hi, @kazougagh is somehow you are making null type into not null type as Kotlin assert that parameter should not be null when type parameter is not-nullable. Can you modify the code as below to see if it still crashes or not

private fun handleDynamicsLink() {
        Firebase.dynamicLinks
            .getDynamicLink(intent)
            .addOnSuccessListener(this) { pendingDynamicLinkData: PendingDynamicLinkData? ->
                // Get deep link from result (may be null if no link is found)
                var deepLink: Uri? = null
                if (pendingDynamicLinkData != null) {
                    deepLink = pendingDynamicLinkData.link
                }
                if (deepLink != null) {
                    handleDeepLink(pendingDynamicLinkData.link!!)
                }
            }
            .addOnFailureListener(this) { e -> PemLog.e("MainActivity - getDynamicLink:onFailure", e) }
    }

You can solve this problem by adding these dependencies.

implementation 'com.google.android.gms:play-services-base:18.0.1'
implementation 'com.google.android.gms:play-services-tasks:18.0.1'

https://developers.google.com/android/guides/releases#december_16_2021