Make Android Lifecycle Observer receiver non nullable in Kotlin












0















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 '18 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 '18 at 6:38
















0















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 '18 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 '18 at 6:38














0












0








0








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 '18 at 6:22









Ruwanka MadhushanRuwanka Madhushan

1,64632037




1,64632037













  • 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 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 '18 at 6:38












2 Answers
2






active

oldest

votes


















3














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






share|improve this answer































    2















    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 '18 at 6:36











    • Yah, there's no other workaround for now :).

      – Jeel Vankhede
      Nov 22 '18 at 6:38











    • Kotlin extension would work, right?

      – Ruwanka Madhushan
      Nov 22 '18 at 6:40






    • 1





      Yes, it works. I'm using that too.

      – Jeel Vankhede
      Nov 22 '18 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',
    autoActivateHeartbeat: false,
    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









    3














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






    share|improve this answer




























      3














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






      share|improve this answer


























        3












        3








        3







        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 '18 at 6:28









        Andrei VinogradovAndrei Vinogradov

        748417




        748417

























            2















            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 '18 at 6:36











            • Yah, there's no other workaround for now :).

              – Jeel Vankhede
              Nov 22 '18 at 6:38











            • Kotlin extension would work, right?

              – Ruwanka Madhushan
              Nov 22 '18 at 6:40






            • 1





              Yes, it works. I'm using that too.

              – Jeel Vankhede
              Nov 22 '18 at 6:40
















            2















            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 '18 at 6:36











            • Yah, there's no other workaround for now :).

              – Jeel Vankhede
              Nov 22 '18 at 6:38











            • Kotlin extension would work, right?

              – Ruwanka Madhushan
              Nov 22 '18 at 6:40






            • 1





              Yes, it works. I'm using that too.

              – Jeel Vankhede
              Nov 22 '18 at 6:40














            2












            2








            2








            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 '18 at 6:34









            Jeel VankhedeJeel Vankhede

            2,3672419




            2,3672419













            • I'd like to have no code at all to check nulls. But this works otherwise, thanks

              – Ruwanka Madhushan
              Nov 22 '18 at 6:36











            • Yah, there's no other workaround for now :).

              – Jeel Vankhede
              Nov 22 '18 at 6:38











            • Kotlin extension would work, right?

              – Ruwanka Madhushan
              Nov 22 '18 at 6:40






            • 1





              Yes, it works. I'm using that too.

              – Jeel Vankhede
              Nov 22 '18 at 6:40



















            • I'd like to have no code at all to check nulls. But this works otherwise, thanks

              – Ruwanka Madhushan
              Nov 22 '18 at 6:36











            • Yah, there's no other workaround for now :).

              – Jeel Vankhede
              Nov 22 '18 at 6:38











            • Kotlin extension would work, right?

              – Ruwanka Madhushan
              Nov 22 '18 at 6:40






            • 1





              Yes, it works. I'm using that too.

              – Jeel Vankhede
              Nov 22 '18 at 6:40

















            I'd like to have no code at all to check nulls. But this works otherwise, thanks

            – Ruwanka Madhushan
            Nov 22 '18 at 6:36





            I'd like to have no code at all to check nulls. But this works otherwise, thanks

            – Ruwanka Madhushan
            Nov 22 '18 at 6:36













            Yah, there's no other workaround for now :).

            – Jeel Vankhede
            Nov 22 '18 at 6:38





            Yah, there's no other workaround for now :).

            – Jeel Vankhede
            Nov 22 '18 at 6:38













            Kotlin extension would work, right?

            – Ruwanka Madhushan
            Nov 22 '18 at 6:40





            Kotlin extension would work, right?

            – Ruwanka Madhushan
            Nov 22 '18 at 6:40




            1




            1





            Yes, it works. I'm using that too.

            – Jeel Vankhede
            Nov 22 '18 at 6:40





            Yes, it works. I'm using that too.

            – Jeel Vankhede
            Nov 22 '18 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.




            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

            "Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

            Alcedinidae

            Origin of the phrase “under your belt”?