0

I have four screens on my app, first one displays a list of messages using a listview, second one has a button with an input that sends messages, third screen connects the first and second screen together and the fourth screen is my getx controller screen where my scroll controller is created

Fourth Screen

class ScrollToTopController extends GetxController {
ScrollController msgScroll = ScrollController();
}

First Screen

final ScrollToTopController sController = Get.put(ScrollToTopController());
//...
Obx(() =>  ListView.builder(
              controller: sController.msgScroll,
              itemCount: chats.length + 1,
              shrinkWrap: true,
              padding: EdgeInsets.only(bottom: 50),
              physics: ScrollPhysics(),
              itemBuilder: (context, index) {
///...

Second Screen

//Button that clicks on this Future below
sendChatData() async {
    if (msg.text == '' && images == null) {
      return;
    }
//Sending chat data to my database and after that do this
if (sController.msgScroll.hasClients) {
      sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
      print("This has client!");
    } else {
      print("This has no client!");
    }
}
//But it always says it doesn't have clients

Third Screen

//inside initState
if (sController.msgScroll.hasClients) {
      sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
      print("This has client!");
    } else {
      print("This has no client!");
    }
//...

//Inside body
Stack(
   children: [
      chatMessages(context, uController, refresh), //First Screen
      ChatBottomInput(cData: widget.chatData),  //Second Screen
    ],
   ),

But the problem am having now is that if i connect my scrollcontroller in my fourth screen to my listview in the first screen using sController.msgScroll it still says it doesn't have a client and the animateTo doesn't work. So is there a way to properly connect it to me listview in order to function well.

If you need more explanation please tell me.

Brightcode
  • 660
  • 9
  • 27

1 Answers1

0

try this

void initState() {
        super.initState();

        WidgetsBinding.instance.addPostFrameCallback((_) {
    if (sController.msgScroll.hasClients) {
          sController.msgScroll.animateTo(0, duration: Duration(milliseconds: 700), curve: Curves.easeInOut);
          print("This has client!");
        } else {
          print("This has no client!");
        }});
      }