Open flodaniel opened 5 months ago
Hi @flodaniel , why do you use the client
itself and not client.currentUserStream
?
The stream here is a custom stream in my app, that I used to resolve issues when a user logged out and a new user logged in. When that happens I create a new StreamChatClient and this stream emits the new client.
I can reproduce a crash on final userId = StreamChat.of(context).currentUser!.id;
in the message_list_view
after a disconnect and reconnet with a new user when using the belwo code. Other operations like queryChannels
work as expected with the new user.
To disconnect:
Future<void> logout() async {
try {
await _tokenRefreshListener?.cancel();
await _badgeListener?.cancel();
final token = await _getDeviceToken();
if (token != null) {
try {
await client.removeDevice(token);
} catch (_) {}
}
await client.disconnectUser(flushChatPersistence: true);
} catch (error, stackTrace) {
unawaited(SentryService().recordError(error, stackTrace: stackTrace));
} finally {
_initialized = null;
}
}
To connect:
Future<OwnUser> _connectUser() async {
final paveUser = _userRepository.user;
final streamUsername =
StreamChatHelper.convertUsernameToStreamUsername(paveUser.username);
final streamChatUser = User(
id: streamUsername,
name: paveUser.displayName,
image: paveUser.username,
);
final tokens = await _authenticationRepository.tokens();
final streamChatToken = tokens.streamChatToken;
return await client.connectUser(
streamChatUser,
streamChatToken,
);
}
Worth to note that the StreamChat
widget is not destroyed when the user logs out and logs in again.
Edit: I fixed it by moving the StreamChat widget further down the tree where it gets destroyed if the user logs out. Not sure if this is still considered a bug then?
Thank you for the clarification @flodaniel , I have yet another question, do you use a different apiKey
for each user? If not I don't see why you have to recreate the StreamChatClient
each time. A connectUser
and a disconnectUser
should work fine. By keeping the same StreamChatClient
instance you can expose its currentUserStream
rather than itself.
We don't. We started recreating it due to issues with the unread messages count.
Which packages are you using?
stream_chat_flutter, stream_chat_persistance, stream_chat_localizations
On what platforms did you experience the issue?
iOS, Android
What version are you using?
stream_chat_flutter: 8.0.0-beta.2 stream_chat_localizations: 8.0.0-beta.2 stream_chat_persistence: 8.0.0-beta.2
What happened?
I have the below code to manage updates to my client (e.g. user logs out and logs in with a different account). If stream emits for the same user again it seems to create a new instance as i then get
StreamChatNetworkError(code: 1000, message: Unauthorised, token not defined)
when calling.watch
on a channel:Steps to reproduce
Supporting info to reproduce
No response
Relevant log output
No response
Flutter analyze output
No response
Flutter doctor output
Code of Conduct