Search code examples
androidflutterdartlistviewscroll

How to add automatic scroll to a StreamBuilder Listview in flutter


I asked this question and got the accurate answer

It worked fine, then I tried wrapping it to StreamBuilder as seen below, it stopped working

StreamBuilder<List<WinsModel>>(
                              stream: WinController().readWins(),
                              builder: (context, snapshot2) {
                                if (snapshot2.hasError) {
                                  return NoData(
                                      text: "Error: 947474774", title: "");
                                } else if (snapshot2.hasData) {
                                  final testi = snapshot2.data!;
                                  return ListView.builder(
                                    controller: _controller,
                                    key: itemKey,
                                    itemCount: testi.length,
                                    itemBuilder: (BuildContext context, index) {
                                      return ListTile(
                                        title: SmallText(
                                          text: testi[index].body,
                                          color: Colors.white,
                                          size: FDiamension.getSize(16),
                                          isBold: true,
                                        ),
                                      );
                                    },
                                  );
                                } else {
                                  return Container();
                                }
                              }),
                   
           

Please which way to add automatic scroll to a StreamBuilder Listview


Solution

  • Put this

    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
          if (_controller.hasClients) {
            _controller.animateTo(_controller.position.maxScrollExtent,
                duration: const Duration(milliseconds: 500),
                curve: Curves.easeInOut);
          }
        });
    

    after

    } else if (snapshot2.hasData) {

    Like

    StreamBuilder<List<WinsModel>>(
                                  stream: WinController().readWins(),
                                  builder: (context, snapshot2) {
                                    if (snapshot2.hasError) {
                                      return NoData(
                                          text: "Error: 947474774", title: "");
                                    } else if (snapshot2.hasData) {
    
        WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
              if (_controller.hasClients) {
                _controller.animateTo(_controller.position.maxScrollExtent,
                    duration: const Duration(milliseconds: 500),
                    curve: Curves.easeInOut);
              }
            });
                                      final testi = snapshot2.data!;
                                      return ListView.builder(
                                        controller: _controller,
                                        key: itemKey,
                                        itemCount: testi.length,
                                        itemBuilder: (BuildContext context, index) {
                                          return ListTile(
                                            title: SmallText(
                                              text: testi[index].body,
                                              color: Colors.white,
                                              size: FDiamension.getSize(16),
                                              isBold: true,
                                            ),
                                          );
                                        },
                                      );
                                    } else {
                                      return Container();
                                    }
                                  }),