0

I am trying the code which I have attached below. But there I am facing the issue like Failed assertion: line 108 pos 12: '_positions.length == 1': ScrollController attached to multiple scroll views.

CustomScrollView(
 controller: scrollController, 
 slivers: [
  SliverList(
   delegate: SliverChildBuilderDelegate(
    (BuildContext context, int index) {
      return Column(
        children: [
          ListTile(
            title: Text('Item $index'),
          ),
          Divider(),
        ],
      );
    },
    childCount: 100,
   ),
  ),
 ],
),



Timer(
    const Duration(milliseconds: 100),
        () => scrollController
        .jumpTo(scrollController.position.maxScrollExtent));

Can anyone tell any solution for this

Vasanthan Raj
  • 126
  • 1
  • 9

2 Answers2

0
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

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

  @override
  State<TestPage> createState() => _TestPageState();
}

class _TestPageState extends State<TestPage> {

  final ScrollController scrollController = ScrollController();

  void scrollList(){
    Future.delayed(const Duration(milliseconds: 100), () {
      if(scrollController.hasClients){
        SchedulerBinding.instance.addPostFrameCallback((_) {
          scrollController.animateTo(
            scrollController.position.maxScrollExtent,
            duration: const Duration(seconds: 5),
            curve: Curves.easeOut,);
        });
      }
    });
  }

  @override
  void initState() {
    super.initState();
    scrollList();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: CustomScrollView(
        controller: scrollController,
        slivers: [
          SliverList(
            delegate: SliverChildBuilderDelegate((BuildContext context, int index) {
                return Column(
                  children: [
                    ListTile(
                      title: Text('Item ${index+1}'),
                    ),
                    const Divider(),
                  ],
                );
              },
              childCount: 100,
            ),
          ),
        ],
      ),
    );
  }
}
Vivek Chib
  • 837
  • 1
  • 2
  • 13
0

Use this instead of using the position

int autoScroll = 1;
Timer timer = Timer.periodic(const Duration(seconds: 2), (timer) {
  autoScroll = index == 0 ? 1 : index == chatData.length -1 ? 1 : autoScroll;
  index += autoScroll;
});

scrollController.animateTo(
    (index * 1000).toDouble(),
    duration: const Duration(milliseconds: 200),
    curve: Curves.easeInOut
);

It is working for me.

Vasanthan Raj
  • 126
  • 1
  • 9