Make Android Lifecycle Observer receiver non nullable in Kotlin











up vote
0
down vote

favorite












I have Result wrapper that wraps data comes from backend



data class Result<T>(val success: Boolean, val result: T?, val message: String?)


Idea of this, check success instead of result being null or not valid and get formatted message for UI error reporting. But when trying to use this with android lifestyle components, specifically in Observer I have to check for null.



enter image description here



How can I avoid this null check? This happens because of



void onChanged(@Nullable T t);


in Observer. I've tried to extend this but it seem to require more custom wrapper classes. Do we have a solution for avoid null check here.










share|improve this question






















  • I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
    – ASN
    Nov 22 at 6:35






  • 1




    Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
    – Ruwanka Madhushan
    Nov 22 at 6:38















up vote
0
down vote

favorite












I have Result wrapper that wraps data comes from backend



data class Result<T>(val success: Boolean, val result: T?, val message: String?)


Idea of this, check success instead of result being null or not valid and get formatted message for UI error reporting. But when trying to use this with android lifestyle components, specifically in Observer I have to check for null.



enter image description here



How can I avoid this null check? This happens because of



void onChanged(@Nullable T t);


in Observer. I've tried to extend this but it seem to require more custom wrapper classes. Do we have a solution for avoid null check here.










share|improve this question






















  • I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
    – ASN
    Nov 22 at 6:35






  • 1




    Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
    – Ruwanka Madhushan
    Nov 22 at 6:38













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have Result wrapper that wraps data comes from backend



data class Result<T>(val success: Boolean, val result: T?, val message: String?)


Idea of this, check success instead of result being null or not valid and get formatted message for UI error reporting. But when trying to use this with android lifestyle components, specifically in Observer I have to check for null.



enter image description here



How can I avoid this null check? This happens because of



void onChanged(@Nullable T t);


in Observer. I've tried to extend this but it seem to require more custom wrapper classes. Do we have a solution for avoid null check here.










share|improve this question













I have Result wrapper that wraps data comes from backend



data class Result<T>(val success: Boolean, val result: T?, val message: String?)


Idea of this, check success instead of result being null or not valid and get formatted message for UI error reporting. But when trying to use this with android lifestyle components, specifically in Observer I have to check for null.



enter image description here



How can I avoid this null check? This happens because of



void onChanged(@Nullable T t);


in Observer. I've tried to extend this but it seem to require more custom wrapper classes. Do we have a solution for avoid null check here.







android kotlin android-lifecycle






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 at 6:22









Ruwanka Madhushan

1,50022036




1,50022036












  • I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
    – ASN
    Nov 22 at 6:35






  • 1




    Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
    – Ruwanka Madhushan
    Nov 22 at 6:38


















  • I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
    – ASN
    Nov 22 at 6:35






  • 1




    Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
    – Ruwanka Madhushan
    Nov 22 at 6:38
















I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
– ASN
Nov 22 at 6:35




I think its mostly because the output(Result<Location>?) can be either success or Null. Therefore it is asking you to add a safe operator check to avoid that. So I think it should be it?.success or you can do a let operator on the result.
– ASN
Nov 22 at 6:35




1




1




Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
– Ruwanka Madhushan
Nov 22 at 6:38




Your right from one perspective, but since we handling the updates of MutableLiveData we can make sure that null is not posted as result
– Ruwanka Madhushan
Nov 22 at 6:38












2 Answers
2






active

oldest

votes

















up vote
3
down vote



accepted










It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.






share|improve this answer




























    up vote
    2
    down vote














    Updated answer from @Andrei Vinogradov's answer




    Until you upgrade to 2.0.0-beta01, you can try this solution. Use standard function let from Kotlin library :



    it?.let{ result ->
    if(result.success){
    // Rest of your code ..
    }
    }





    share|improve this answer





















    • I'd like to have no code at all to check nulls. But this works otherwise, thanks
      – Ruwanka Madhushan
      Nov 22 at 6:36










    • Yah, there's no other workaround for now :).
      – Jeel Vankhede
      Nov 22 at 6:38










    • Kotlin extension would work, right?
      – Ruwanka Madhushan
      Nov 22 at 6:40






    • 1




      Yes, it works. I'm using that too.
      – Jeel Vankhede
      Nov 22 at 6:40











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424967%2fmake-android-lifecycle-observer-receiver-non-nullable-in-kotlin%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    3
    down vote



    accepted










    It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.






    share|improve this answer

























      up vote
      3
      down vote



      accepted










      It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.






      share|improve this answer























        up vote
        3
        down vote



        accepted







        up vote
        3
        down vote



        accepted






        It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.






        share|improve this answer












        It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 22 at 6:28









        Andrei Vinogradov

        584316




        584316
























            up vote
            2
            down vote














            Updated answer from @Andrei Vinogradov's answer




            Until you upgrade to 2.0.0-beta01, you can try this solution. Use standard function let from Kotlin library :



            it?.let{ result ->
            if(result.success){
            // Rest of your code ..
            }
            }





            share|improve this answer





















            • I'd like to have no code at all to check nulls. But this works otherwise, thanks
              – Ruwanka Madhushan
              Nov 22 at 6:36










            • Yah, there's no other workaround for now :).
              – Jeel Vankhede
              Nov 22 at 6:38










            • Kotlin extension would work, right?
              – Ruwanka Madhushan
              Nov 22 at 6:40






            • 1




              Yes, it works. I'm using that too.
              – Jeel Vankhede
              Nov 22 at 6:40















            up vote
            2
            down vote














            Updated answer from @Andrei Vinogradov's answer




            Until you upgrade to 2.0.0-beta01, you can try this solution. Use standard function let from Kotlin library :



            it?.let{ result ->
            if(result.success){
            // Rest of your code ..
            }
            }





            share|improve this answer





















            • I'd like to have no code at all to check nulls. But this works otherwise, thanks
              – Ruwanka Madhushan
              Nov 22 at 6:36










            • Yah, there's no other workaround for now :).
              – Jeel Vankhede
              Nov 22 at 6:38










            • Kotlin extension would work, right?
              – Ruwanka Madhushan
              Nov 22 at 6:40






            • 1




              Yes, it works. I'm using that too.
              – Jeel Vankhede
              Nov 22 at 6:40













            up vote
            2
            down vote










            up vote
            2
            down vote










            Updated answer from @Andrei Vinogradov's answer




            Until you upgrade to 2.0.0-beta01, you can try this solution. Use standard function let from Kotlin library :



            it?.let{ result ->
            if(result.success){
            // Rest of your code ..
            }
            }





            share|improve this answer













            Updated answer from @Andrei Vinogradov's answer




            Until you upgrade to 2.0.0-beta01, you can try this solution. Use standard function let from Kotlin library :



            it?.let{ result ->
            if(result.success){
            // Rest of your code ..
            }
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 22 at 6:34









            Jeel Vankhede

            2,0461216




            2,0461216












            • I'd like to have no code at all to check nulls. But this works otherwise, thanks
              – Ruwanka Madhushan
              Nov 22 at 6:36










            • Yah, there's no other workaround for now :).
              – Jeel Vankhede
              Nov 22 at 6:38










            • Kotlin extension would work, right?
              – Ruwanka Madhushan
              Nov 22 at 6:40






            • 1




              Yes, it works. I'm using that too.
              – Jeel Vankhede
              Nov 22 at 6:40


















            • I'd like to have no code at all to check nulls. But this works otherwise, thanks
              – Ruwanka Madhushan
              Nov 22 at 6:36










            • Yah, there's no other workaround for now :).
              – Jeel Vankhede
              Nov 22 at 6:38










            • Kotlin extension would work, right?
              – Ruwanka Madhushan
              Nov 22 at 6:40






            • 1




              Yes, it works. I'm using that too.
              – Jeel Vankhede
              Nov 22 at 6:40
















            I'd like to have no code at all to check nulls. But this works otherwise, thanks
            – Ruwanka Madhushan
            Nov 22 at 6:36




            I'd like to have no code at all to check nulls. But this works otherwise, thanks
            – Ruwanka Madhushan
            Nov 22 at 6:36












            Yah, there's no other workaround for now :).
            – Jeel Vankhede
            Nov 22 at 6:38




            Yah, there's no other workaround for now :).
            – Jeel Vankhede
            Nov 22 at 6:38












            Kotlin extension would work, right?
            – Ruwanka Madhushan
            Nov 22 at 6:40




            Kotlin extension would work, right?
            – Ruwanka Madhushan
            Nov 22 at 6:40




            1




            1




            Yes, it works. I'm using that too.
            – Jeel Vankhede
            Nov 22 at 6:40




            Yes, it works. I'm using that too.
            – Jeel Vankhede
            Nov 22 at 6:40


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53424967%2fmake-android-lifecycle-observer-receiver-non-nullable-in-kotlin%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks