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.
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 |
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.
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 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 at 6:38
add a comment |
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.
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 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 beit?.success
or you can do alet
operator on the result.
– ASN
Nov 22 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 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 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 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
add a comment |
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.
add a comment |
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 ..
}
}
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
add a comment |
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.
add a comment |
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.
add a comment |
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.
It's a framework bug that argument is annotated as @Nullable. Fixed in androix.lifecycle 2.0.0-beta01.
answered Nov 22 at 6:28
Andrei Vinogradov
584316
584316
add a comment |
add a comment |
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 ..
}
}
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
add a comment |
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 ..
}
}
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
add a comment |
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 ..
}
}
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 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
add a comment |
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
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.
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.
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 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 at 6:38