stream-chat-flutter icon indicating copy to clipboard operation
stream-chat-flutter copied to clipboard

You cannot use query channels without active connections

Open Peeyush14198 opened this issue 4 years ago • 10 comments

Which packages are you using?

stream_chat_flutter_core

On what platforms did you experience the issue?

iOS, Android

What version are you using?

stream_chat_flutter_core : 3.4.0

What happened?

When logging out of the app we call streamChatClient.disconnectUser(flushChatPersistence: true)

When we log in again we call connect user as mentioned in the documentation but we get an error message when querying channels by using the inbuilt widget ChannelsBloc, ChannelListCore that you cannot query channels without an active connection.

The user is connected as we are able to get an unread count of messages using streamChatClient!.state.unreadChannelsStream and also in the get stream chat client the client / state shows that user is present

Steps to reproduce

1. call await streamChatClient.disconnectUser(flushChatPersistence: true)
2. call await streamChatClient.connectUser(
              StreamChatSdk.User(
                id: ${new user id},
              
              ),
              ${user token },
            )
3. ChannelsBloc(
      child: ChannelListCore(
        watch: true,
        channelListController: ${your controller}
        filter: Filter.and([
          Filter.equal('type', 'messaging'),
          Filter.in_(
            'members',
            [
              ${user_id},
            ],
          )
        ]),
        emptyBuilder: (BuildContext context) => EmptyScreenWidget(
          imagePath: kImageNoMessages,
          title: noMessagesScreenTitle,
          subtitle: noMessagesScreenSubtitle,
          buttonImagePath: kImageStartConversationButton,
          onButtonImageTap: () => ${your function}
        ),
        loadingBuilder: (BuildContext context) => const Center(
          child: Loader(
            useLottieLoader: true,
          ),
        ),
        errorBuilder: (
          BuildContext context,
          dynamic error,
        ) =>
            Center(
          child: Text(error.toString(), style: kTextStyleMetropolisBody1),
        ),
        listBuilder: (
          BuildContext context,
          List<Channel> channels,
        ) =>
            LazyLoadScrollView(
          onEndOfPage: () async {
           
          },
          child: ListView.separated(
            itemCount: channels.length,
            separatorBuilder: (context, index) {
              return const Divider();
            },
            itemBuilder: (BuildContext context, int index) {
               return ${your function}
            },
          ),
        ),
      ),
    )

Supporting info to reproduce

No response

Relevant log output

No response

Flutter analyze output

No response

Flutter doctor output

No response

Code of Conduct

  • [X] I agree to follow this project's Code of Conduct

Peeyush14198 avatar Feb 21 '22 07:02 Peeyush14198

Hey @Peeyush14198, it looks like you are not awaiting the connectUser call. Can you share a sample project where we can reproduce this?

xsahil03x avatar Feb 21 '22 07:02 xsahil03x

Hi @xsahil03x I am actually awaiting for the connect user

await getStreamHelper.streamChatClient!.connectUser(
          StreamChatSdk.User(
            id: userHelper.getUserInfo()!.hasuraUserId!,
            extraData: userHelper.buildUserGetStreamInfo(userHelper.getUserInfo()!),
          ),
          userHelper.getUserInfo()!.getStreamChatToken!,
        );

Peeyush14198 avatar Feb 21 '22 08:02 Peeyush14198

@Peeyush14198 , I am not able to reproduce with the mentioned steps. Can you share a quick gist where I can test?

xsahil03x avatar Feb 21 '22 08:02 xsahil03x

To reproduce you can try logging out and logging in with different users

and also for building the list you use this

ChannelsBloc(
      child: ChannelListCore(
        watch: true,
        channelListController: ${your controller}
        filter: Filter.and([
          Filter.equal('type', 'messaging'),
          Filter.in_(
            'members',
            [
              ${user_id},
            ],
          )
        ]),
        emptyBuilder: (BuildContext context) => EmptyScreenWidget(
          imagePath: kImageNoMessages,
          title: noMessagesScreenTitle,
          subtitle: noMessagesScreenSubtitle,
          buttonImagePath: kImageStartConversationButton,
          onButtonImageTap: () => ${your function}
        ),
        loadingBuilder: (BuildContext context) => const Center(
          child: Loader(
            useLottieLoader: true,
          ),
        ),
        errorBuilder: (
          BuildContext context,
          dynamic error,
        ) =>
            Center(
          child: Text(error.toString(), style: kTextStyleMetropolisBody1),
        ),
        listBuilder: (
          BuildContext context,
          List<Channel> channels,
        ) =>
            LazyLoadScrollView(
          onEndOfPage: () async {
           ${your_fucntion}
          },
          child: ListView.separated(
            itemCount: channels.length,
            separatorBuilder: (context, index) {
              return const Divider();
            },
            itemBuilder: (BuildContext context, int index) {
               return ${your function}
            },
          ),
        ),
      ),
    )

Peeyush14198 avatar Feb 21 '22 08:02 Peeyush14198

@Peeyush14198 Can you also share the widget code, where you are calling connect and disconnect user?

xsahil03x avatar Feb 21 '22 08:02 xsahil03x

@xsahil03x for disconnect it is an action performed on logout whereas for connect we call it under our init state of the home screen and await till it gets connected

 await context.read<GetStreamProvider>().connectStreamChatUser();
 Future<OwnUser?> connectStreamChatUser() async {
    try {
      if (getStreamHelper.streamChatClient!.state.currentUser == null ||
          getStreamHelper.streamChatClient!.state.currentUser?.id != userHelper.getUserInfo()!.hasuraUserId) {
        return await getStreamHelper.streamChatClient!.connectUser(
          StreamChatSdk.User(
            id: userHelper.getUserInfo()!.hasuraUserId!,
            extraData: userHelper.buildUserGetStreamInfo(userHelper.getUserInfo()!),
          ),
          userHelper.getUserInfo()!.getStreamChatToken!,
        );
      }
 
    } catch (e) {
      rethrow
    }
  }

thus this seems correct?

Peeyush14198 avatar Feb 21 '22 08:02 Peeyush14198

@Peeyush14198 Is the ChannelListCore in the same HomeScreen or do you navigate to the ChannelScreen once the connection is successful?

If it's in the same screen, can you share the snippet of the whole widget?

xsahil03x avatar Feb 21 '22 08:02 xsahil03x

Um No, actually it is a different bottom tab once the connection is established we allow users to navigate

  1. So in the home screen, under init state, we call connect get stream and wait till it connects once connected we display the whole UI
  2. We have a bottom tab for a chat when on clicking it we build ChannelListCore

Please consider this flow ---

so > main.dart has

MaterialApp(
      title: 'Leafs',
      builder: (context, child) => StreamChatCore(
        client: getStreamHelper.initGetStreamChatSdk(),
        child: child!,
      ),
      debugShowCheckedModeBanner: false,
      theme: Themes.instance().lightTheme,
      home: SafeAreaWidget(child: const RootScreen()),
    )
    

home .dart


initState (){
try{
OwnUser user =  await context.read<GetStreamProvider>().connectStreamChatUser();
loader = false
setState
}
catch (e){
// do something on error
}

}

return  loader ? Loader() : Scaffold(
bottomNavigationBar ---------
body : index == 0 ? HomeScreen(

) : ChatScreen
)

get-stream-provider.dart

Future<OwnUser?> connectStreamChatUser() async {
    try {
      if (getStreamHelper.streamChatClient!.state.currentUser == null ||
          getStreamHelper.streamChatClient!.state.currentUser?.id != userHelper.getUserInfo()!.hasuraUserId) {
        return await getStreamHelper.streamChatClient!.connectUser(
          StreamChatSdk.User(
            id: userHelper.getUserInfo()!.hasuraUserId!,
            extraData: userHelper.buildUserGetStreamInfo(userHelper.getUserInfo()!),
          ),
          userHelper.getUserInfo()!.getStreamChatToken!,
        );
      }
    
    } catch (e) {
         rethrow
    }
  }

chat. dart (screen )

same as channel code already shared above for channel bloc

account.dart

logout button - > await getStreamHelper.logoutGetStreamChatSdk();

get-stream-helper.dart

 StreamChatSdk.StreamChatClient? streamChatClient;
 initGetStreamChatSdk() {
    if (streamChatClient == null) {
      streamChatClient = StreamChatSdk.StreamChatClient(Config.instance().getStreamFeedApiKey, logLevel: Level.SEVERE);
    }
    return streamChatClient;
  }
  
  Future<void> logoutGetStreamChatSdk() async {
    await streamChatClient!.disconnectUser();
    streamChatClient = null;
  }

I cannot share the whole code as it is private cooperate code though try my best to assist you

Peeyush14198 avatar Feb 21 '22 08:02 Peeyush14198

@Peeyush14198 , I am still not able to reproduce the issue. Can you please share a small project on GitHub where we can replicate it? No need to share the credentials, We can use ours.

xsahil03x avatar Feb 22 '22 10:02 xsahil03x

Without additional information, we are unfortunately not sure how to resolve this issue. We are therefore reluctantly going to close this bug for now. Please don't hesitate to comment on the bug if you have any more information for us; we will reopen it right away! Thanks for your contribution.

no-response[bot] avatar Mar 14 '22 13:03 no-response[bot]