Make Android Lifecycle Observer receiver non nullable in Kotlin
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.
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
add a comment |
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.
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
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 beit?.success
or you can do alet
operator on the result.
– ASN
Nov 22 '18 at 6:35
1
Your right from one perspective, but since we handling the updates ofMutableLiveData
we can make sure thatnull
is not posted as result
– Ruwanka Madhushan
Nov 22 '18 at 6:38
add a comment |
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.
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
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.
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
android kotlin android-lifecycle
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 beit?.success
or you can do alet
operator on the result.
– ASN
Nov 22 '18 at 6:35
1
Your right from one perspective, but since we handling the updates ofMutableLiveData
we can make sure thatnull
is not posted as result
– Ruwanka Madhushan
Nov 22 '18 at 6:38
add a comment |
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 beit?.success
or you can do alet
operator on the result.
– ASN
Nov 22 '18 at 6:35
1
Your right from one perspective, but since we handling the updates ofMutableLiveData
we can make sure thatnull
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
add a comment |
2 Answers
2
active
oldest
votes
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
add a comment |
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 ..
}
}
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
add a comment |
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
add a comment |
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
answered Nov 22 '18 at 6:28
Andrei VinogradovAndrei Vinogradov
748417
748417
add a comment |
add a comment |
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 ..
}
}
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
add a comment |
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 ..
}
}
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
add a comment |
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 ..
}
}
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 ..
}
}
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 alet
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 thatnull
is not posted as result– Ruwanka Madhushan
Nov 22 '18 at 6:38