Channels List realtime updates not working properly in StreamBuilder

Any changes to channels is not updating in real time (no callback) on the client end

We have to refresh the screen & fetch fresh data manually for retrieving the updated data (channels)

Please suggest & specify the handlers or alternatives for auto-updation on the client screen

Hello @rpamity You can check out the “Query and Filter Messages” section in the Amity SDK documentation for detailed info and code examples. The link will guide you on setting up auto-updates on your client screen. If you have any issues or questions, feel free to reach out for help!

Thanks for your revert we kindly noticed that we are receiving messages as per the given documentation.The actual issue lies with channels list.Whenever a new channel is created the client does not get the realtime update we have to fetch the updated channel manually this must be automated.

Hello @rpamity , It seems the problem is with real-time updates for the channels list, especially when creating a new channel. To automate this process, you can refer to the documentation on querying channels. Check out the details and examples in the “Query Channels” section here. If you have any more questions or run into issues, feel free to reach out for further help.

Thanks for your revert we implemented the suggested solution but we still facing the same issue.

This is our

amityUsers() {
_controller = PagingController(
pageFuture: (token) =>
AmityChatClient.newChannelRepository()
.getChannels().types(_type).communityType()
.withKeyword(_keyboard.isEmpty ? null : _keyboard)
.sortBy(_sort)
.filter(_filter)
.includingTags(_tags ?? )
.excludingTags(_excludingTags ?? )
.includeDeleted(false).liveType()
.getPagingData(token: token, limit: 20),
pageSize: 20,
);
postData.clear();
amityChannel.clear();
_controller.addListener(() async {
for (var v in _controller.loadedItems) {
amityChannel.add(v);

    postData.add(v.channelId);
    print("fhffff:${amityChannel[0].displayName}");
  }
  Future.delayed(Duration(seconds: 5), () {
    future = getChatUser();
  });
});
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
  _controller.fetchNextPage();
  update();
});

for fetching the data

and this code is for viewing the data

Expanded(
child:
ListView.builder(itemCount: messageController.amityChannel.length ,itemBuilder: (context,index){
return StreamBuilder(stream: messageController.amityChannel[index].listen.stream, builder:
(context, snapshot) {
// Check the connection state of the stream
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
} else if (snapshot.hasError) {

          return Text('Error: ${snapshot.error}');
        } else if (!snapshot.hasData || snapshot.data == null) {

          return  Column(mainAxisAlignment: MainAxisAlignment.center,children: [
        Image.asset("assets/empty_mess.png",height: height*.25,),
        SizedBox(height: height*.02,),
        Padding(
          padding:  EdgeInsets.symmetric(horizontal:width*.15,vertical: height*.02 ),
          child: Text('''You have not initiated conversation with any profile.Start ''',
            style: TextStyle(color: mainColor,fontFamily: "inter_reg",fontSize: Constants().textSize),textAlign: TextAlign.center,),),
            // Constants().gradientButton("View Profile", () { })
      ],);

        } else {
          return Text(snapshot.data!.channelId??"");
        }
                }

                );
              })

Hello @rpamity , From the issue you’ve encountered, our team has reviewed your code and we recommend that you try using this code.

import 'package:amity_sdk/amity_sdk.dart';
import 'package:flutter/material.dart';
import 'package:flutter_social_sample_app/core/constant/global_constant.dart';
import 'package:flutter_social_sample_app/core/route/app_route.dart';
import 'package:flutter_social_sample_app/core/utils/debouncer.dart';
import 'package:flutter_social_sample_app/core/widget/channel_widget.dart';
import 'package:flutter_social_sample_app/core/widget/dialog/edit_text_dialog.dart';
import 'package:flutter_social_sample_app/core/widget/dialog/error_dialog.dart';
import 'package:go_router/go_router.dart';

class ChannelListScreen extends StatefulWidget {
  const ChannelListScreen({Key? key}) : super(key: key);

  @override
  State<ChannelListScreen> createState() => _ChannelListScreenState();
}

class _ChannelListScreenState extends State<ChannelListScreen> {
  late PagingController<AmityChannel> _controller;
  final amityCommunities = <AmityChannel>[];

  final scrollcontroller = ScrollController();
  bool loading = false;

  String _keyboard = '';

  final _debouncer = Debouncer(milliseconds: 500);

  AmityChannelFilter _filter = AmityChannelFilter.ALL;
  final List<AmityChannelType> _type = [];
  AmityChannelSortOption _sort = AmityChannelSortOption.LAST_ACTIVITY;
  List<String>? _tags;
  List<String>? _excludingTags;

  @override
  void initState() {
    _controller = PagingController(
      pageFuture: (token) => AmityChatClient.newChannelRepository()
          .getChannels()
          .withKeyword(_keyboard.isEmpty ? null : _keyboard)
          .sortBy(_sort)
          .filter(_filter)
          .types(_type)
          .includingTags(_tags ?? [])
          .excludingTags(_excludingTags ?? [])
          .includeDeleted(false)
          .getPagingData(token: token, limit: GlobalConstant.pageSize),
      pageSize: GlobalConstant.pageSize,
    )..addListener(
        () {
          if (_controller.error == null) {
            setState(() {
              amityCommunities.clear();
              amityCommunities.addAll(_controller.loadedItems);
            });
          } else {
            //Error on pagination controller
            setState(() {});
            print(_controller.error.toString());
            print(_controller.stacktrace.toString());
            ErrorDialog.show(context,
                title: 'Error', message: _controller.error.toString());
          }
        },
      );

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _controller.fetchNextPage();
    });

    scrollcontroller.addListener(pagination);

    super.initState();
  }

  void pagination() {
    if ((scrollcontroller.position.pixels ==
            scrollcontroller.position.maxScrollExtent) &&
        _controller.hasMoreItems) {
      setState(() {
        _controller.fetchNextPage();
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Channel List ')),
      body: Column(
        children: [
          Container(
            padding: const EdgeInsets.all(12),
            child: TextFormField(
              onChanged: (value) {
                _debouncer.run(() {
                  _keyboard = value;
                  _controller.reset();
                  _controller.fetchNextPage();
                });
              },
              decoration: const InputDecoration(hintText: 'Enter Keybaord'),
            ),
          ),
          Container(
            child: Row(
              children: [
                Container(
                  padding: const EdgeInsets.all(8),
                  child: PopupMenuButton(
                    itemBuilder: (context) {
                      return [
                        PopupMenuItem(
                          value: 1,
                          child: Text(AmityChannelFilter.ALL.name),
                        ),
                        PopupMenuItem(
                          value: 2,
                          child: Text(AmityChannelFilter.MEMBER.name),
                        ),
                        PopupMenuItem(
                          value: 3,
                          child: Text(AmityChannelFilter.NOT_MEMBER.name),
                        )
                      ];
                    },
                    child: const Icon(
                      Icons.filter_alt_rounded,
                      size: 18,
                    ),
                    onSelected: (index) {
                      if (index == 1) {
                        _filter = AmityChannelFilter.ALL;
                      }
                      if (index == 2) {
                        _filter = AmityChannelFilter.MEMBER;
                      }
                      if (index == 3) {
                        _filter = AmityChannelFilter.NOT_MEMBER;
                      }

                      _controller.reset();
                      _controller.fetchNextPage();
                    },
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(8),
                  child: PopupMenuButton(
                    itemBuilder: (context) {
                      return [
                        CheckedPopupMenuItem(
                          value: 0,
                          checked: _type.isEmpty,
                          child: const Text('ALL'),
                        ),
                        CheckedPopupMenuItem(
                          value: 1,
                          checked: _type.contains(AmityChannelType.COMMUNITY),
                          child: Text(AmityChannelType.COMMUNITY.value),
                        ),
                        CheckedPopupMenuItem(
                          value: 2,
                          checked: _type.contains(AmityChannelType.LIVE),
                          child: Text(AmityChannelType.LIVE.value),
                        ),
                        CheckedPopupMenuItem(
                          value: 3,
                          checked: _type.contains(AmityChannelType.BROADCAST),
                          child: Text(AmityChannelType.BROADCAST.value),
                        ),
                        CheckedPopupMenuItem(
                          value: 4,
                          checked:
                              _type.contains(AmityChannelType.CONVERSATION),
                          child: Text(AmityChannelType.CONVERSATION.value),
                        )
                      ];
                    },
                    child: const Icon(
                      Icons.file_present_rounded,
                      size: 18,
                    ),
                    onSelected: (index) {
                      if (index == 0) {
                        _type.clear();
                      }

                      if (index == 1) {
                        if (_type.contains(AmityChannelType.COMMUNITY)) {
                          _type.remove(AmityChannelType.COMMUNITY);
                        } else {
                          _type.add(AmityChannelType.COMMUNITY);
                        }
                      }
                      if (index == 2) {
                        if (_type.contains(AmityChannelType.LIVE)) {
                          _type.remove(AmityChannelType.LIVE);
                        } else {
                          _type.add(AmityChannelType.LIVE);
                        }
                      }
                      if (index == 3) {
                        if (_type.contains(AmityChannelType.BROADCAST)) {
                          _type.remove(AmityChannelType.BROADCAST);
                        } else {
                          _type.add(AmityChannelType.BROADCAST);
                        }
                      }
                      if (index == 4) {
                        if (_type.contains(AmityChannelType.CONVERSATION)) {
                          _type.remove(AmityChannelType.CONVERSATION);
                        } else {
                          _type.add(AmityChannelType.CONVERSATION);
                        }
                      }

                      _controller.reset();
                      _controller.fetchNextPage();
                    },
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(8),
                  child: PopupMenuButton(
                    itemBuilder: (context) {
                      return [
                        PopupMenuItem(
                          value: 1,
                          child:
                              Text(AmityChannelSortOption.LAST_ACTIVITY.name),
                        ),
                      ];
                    },
                    child: const Icon(
                      Icons.sort_rounded,
                      size: 18,
                    ),
                    onSelected: (index) {
                      if (index == 1) {
                        _sort = AmityChannelSortOption.LAST_ACTIVITY;
                      }

                      _controller.reset();
                      _controller.fetchNextPage();
                    },
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(8),
                  child: InkWell(
                    child: const Icon(Icons.tag, size: 18),
                    onTap: () {
                      EditTextDialog.show(context,
                          title: 'Enter tags, separate by comma',
                          hintText: 'type tags here', onPress: (value) {
                        if (value.isNotEmpty) {
                          _tags = value.trim().split(',');
                        }
                        if (value.isEmpty) {
                          _tags = [];
                        }
                        _controller.reset();
                        _controller.fetchNextPage();
                      });
                    },
                  ),
                ),
                Container(
                  padding: const EdgeInsets.all(8),
                  child: InkWell(
                    child: const Icon(Icons.tag, size: 18),
                    onTap: () {
                      EditTextDialog.show(context,
                          title: 'Enter excluding tags, separate by comma',
                          hintText: 'type tags here', onPress: (value) {
                        if (value.isNotEmpty) {
                          _excludingTags = value.trim().split(',');
                        }
                        if (value.isEmpty) {
                          _excludingTags = [];
                        }
                        _controller.reset();
                        _controller.fetchNextPage();
                      });
                    },
                  ),
                )
              ],
            ),
          ),
          Expanded(
            child: amityCommunities.isNotEmpty
                ? RefreshIndicator(
                    onRefresh: () async {
                      _controller.reset();
                      _controller.fetchNextPage();
                    },
                    child: ListView.builder(
                      controller: scrollcontroller,
                      itemCount: amityCommunities.length,
                      itemBuilder: (context, index) {
                        final amityChannel = amityCommunities[index];
                        return Container(
                          margin: const EdgeInsets.all(12),
                          child: ChannelWidget(
                            amityChannel: amityChannel,
                            // onCommentCallback: () {
                            //   // GoRouter.of(context).goNamed('commentChannelFeed',
                            //   //     params: {'postId': amityPost.postId!});
                            // },
                          ),
                        );
                      },
                    ),
                  )
                : Container(
                    alignment: Alignment.center,
                    child: _controller.isFetching
                        ? const CircularProgressIndicator()
                        : const Text('No Post'),
                  ),
          ),
          if (_controller.isFetching && amityCommunities.isNotEmpty)
            Container(
              alignment: Alignment.center,
              child: const CircularProgressIndicator(),
            )
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          GoRouter.of(context).pushNamed(AppRoute.createChannel);
        },
        child: const Icon(Icons.add),
      ),
    );
  }
}