Issue
I try to get lastLocation of user and implemented the method like this.
getLocationClient()?.lastLocation?.addOnSuccessListener(object :
OnSuccessListener<Location> {
override fun onSuccess(loc: Location) {
if (loc == null) return
}
})
But when I don't add null safety to parameter Location in onSuccess method, that causes some crashes when Location parameter is null.
When I add null safety to the parameter like in the image below, it doesn't override the method. I was able to override this method with null safety but something changed i guess.
How can i solve this problem ?
Solution
The type parameter for OnSuccessListener needs to be nullable too, i.e. OnSuccessListener<Location?>
instead of OnSuccessListener<Location>
getLocationClient()?.lastLocation?.addOnSuccessListener(object :
OnSuccessListener<Location?> {
override fun onSuccess(loc: Location?) {
if (loc == null) return
}
})
BTW, you can also use the SAM syntax for a shorter way of doing the same thing:
getLocationClient()?.lastLocation?.addOnSuccessListener { loc: Location? ->
if (loc == null) return
}
Answered By - Maurice Lam
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.