void can not converted to Unit when use kotlin in java code - android

Viewed 3202

I am working with kotlin and java together in my project, I have a class kotlin like bellow:

class AuthenticationPresenter @Inject constructor(
    private val navigator: AuthenticationNavigator,
    private val getCurrentServerInteractor: GetCurrentServerInteractor,
    private val getAccountInteractor: GetAccountInteractor,
    private val settingsRepository: SettingsRepository,
    private val localRepository: LocalRepository,
    private val tokenRepository: TokenRepository
) {
    suspend fun loadCredentials(newServer: Boolean, callback: (authenticated: Boolean) -> Unit) {
        val currentServer = getCurrentServerInteractor.get()
        val serverToken = currentServer?.let { tokenRepository.get(currentServer) }
        val settings = currentServer?.let { settingsRepository.get(currentServer) }
        val account = currentServer?.let { getAccountInteractor.get(currentServer) }

        account?.let {
            localRepository.save(LocalRepository.CURRENT_USERNAME_KEY, account.userName)
        }

        if (newServer || currentServer == null || serverToken == null || settings == null || account?.userName == null) {
            callback(false)
        } else {
            callback(true)
            navigator.toChatList()
        }
    }
}

I am converting bellow code (kotlin) to java:

presenter.loadCredentials(newServer || deepLinkInfo != null) { authenticated ->
    if (!authenticated) {
        showServerInput(deepLinkInfo)
    }
}

And this is my convert code to java but get me error:

presenter.loadCredentials((newServer || deepLinkInfo != null), authenticated ->{
    if (!authenticated) {
        showServerInput(deepLinkInfo);
    }
});

But say me: Missing return statement

What can I use from this loadCredentials in java code?

Code of showServerInput:

fun showServerInput(deepLinkInfo: LoginDeepLinkInfo?) {
    addFragment(TAG_SERVER_FRAGMENT, R.id.fragment_container, allowStateLoss = true) {
        ServerFragment.newInstance(deepLinkInfo)
    }
}
3 Answers

All the previous answers fail to include the part where you need to return an instance of Unit, once I did that I was able to get a similar issue fixed.

There is a slight issue with the original question in that "AA" cannot be returned and so would throw an error and instead you'd need to call a method.

Here's how to do it in the context of this question.

presenter.loadCredentials((newServer || deepLinkInfo != null), authenticated ->{
    if (!authenticated) {
        showServerInput(deepLinkInfo); // Here!
    } else {
        myOtherMethod();
    }
    return Unit.INSTANCE;
});

You need to return the result of showServerInput:

presenter.loadCredentials((newServer || deepLinkInfo != null), authenticated ->{
    if (!authenticated) {
        return showServerInput(deepLinkInfo); // Here!
    } else {
        return "AA";
    }
});

Try this

presenter.loadCredentials((newServer || deepLinkInfo != null),   authenticated ->{
    if (!authenticated) {
        showServerInput(deepLinkInfo);
    }
});
Related