jo jo
jo jo

Reputation: 1838

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

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)
    }
}

Upvotes: 9

Views: 5540

Answers (3)

Mark O'Sullivan
Mark O'Sullivan

Reputation: 10778

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;
});

Upvotes: 7

EpicPandaForce
EpicPandaForce

Reputation: 81539

Try this

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

Upvotes: 1

Mureinik
Mureinik

Reputation: 311053

You need to return the result of showServerInput:

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

Upvotes: 1

Related Questions