GetStream/compose-chat-tutorial

bug: throws `IllegalStateException`

Closed this issue · 1 comments

Cloning this repository, and then running it without changing any code, results in the following error:

java.lang.IllegalStateException: ChatClient::connectUser() must be called before resolving any dependency
 at io.getstream.chat.android.state.extensions.ChatClientExtensions.getGlobalState(ChatClient.kt:460)
 at io.getstream.chat.android.compose.viewmodel.channels.ChannelListViewModel.<init>(ChannelListViewModel.kt:139)
 at io.getstream.chat.android.compose.viewmodel.channels.ChannelViewModelFactory$factories$1.invoke(ChannelViewModelFactory.kt:53)
 at io.getstream.chat.android.compose.viewmodel.channels.ChannelViewModelFactory$factories$1.invoke(ChannelViewModelFactory.kt:52)
 at io.getstream.chat.android.compose.viewmodel.channels.ChannelViewModelFactory.create(ChannelViewModelFactory.kt:69)
 at androidx.lifecycle.ViewModelProvider$Factory.create(ViewModelProvider.kt:83)
 ...

If I change this part:

client.connectUser(
    user = user,
    token = ""
).enqueue()

// 4 - Set up the Channels Screen UI
setContent {
    ChatTheme {
        ChannelsScreen(
            title = stringResource(id = R.string.app_name),
            isShowingSearch = true,
            onItemClick = { channel ->
                startActivity(MessagesActivity4.getIntent(this, channel.cid))
            },
            onBackPressed = { finish() }
        )
    }
}

to:

client.connectUser(
    user = user,
    token = ""
).enqueue { result ->
    if (result.isSuccess) {
        // 4 - Set up the Channels Screen UI
        setContent {
            ChatTheme {
                ChannelsScreen(
                    title = stringResource(id = R.string.app_name),
                    isShowingSearch = true,
                    onItemClick = { channel ->
                        startActivity(MessagesActivity4.getIntent(this, channel.cid))
                    },
                    onBackPressed = { finish() }
                )
            }
        }
    }
}

it works perfectly fine. Is that an error in the code? Or what's going on here?

Yes, that's correct. We had an issue in the sample and your solution is correct. We have fixed it recently.
A more practical solution is to listen to the initialization state of the SDK and draw the UI based on that, I have just opend a PR with the change: https://github.com/GetStream/compose-chat-tutorial/pull/20/files

Thanks for reporting this.