Bloc: ScrollController not attached to any scroll views.

Created on 3 Sep 2020  路  5Comments  路  Source: felangel/bloc

E/flutter (13373): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 112 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

class ArticlesList extends StatefulWidget {
  @override
  _ArticlesListState createState() => _ArticlesListState();
}

class _ArticlesListState extends State<ArticlesList> {
  final _scrollController = ScrollController();
  ArticleBloc _articleBloc;

  @override
  void initState() {
    super.initState();
    _scrollController.addListener(_onScroll);
    _articleBloc = context.bloc<ArticleBloc>();
  }

  @override
  Widget build(BuildContext context) {
    return BlocConsumer<ArticleBloc, ArticleState>(
      listener: (context, state) {
        if (!state.hasReachedMax && _isBottom) {
          _articleBloc.add(Fetch());
        }
      },
      builder: (context, state) {
        switch (state.status) {
          case ArticleStatus.failure:
            return const Center(
                child: Text("袙芯蟹薪懈泻谢懈 锌褉芯斜谢械屑褘 褋 蟹邪谐褉褍蟹泻芯泄 馃槙"));
          case ArticleStatus.success:
            if (state.articles.isEmpty) {
              return const Center(child: Text('小褌邪褌械泄 锌芯泻邪 薪械褌...'));
            }
            return ListView.builder(
              physics: BouncingScrollPhysics(),
              itemBuilder: (BuildContext context, int index) {
                return index >= state.articles.length
                    ? BottomLoader()
                    : ArticleCardItem(article: state.articles[index]);
              },
              itemCount: state.hasReachedMax
                  ? state.articles.length
                  : state.articles.length + 1,
              controller: _scrollController,
            );
          default:
            return const Center(child: CircularProgressIndicator());
        }
      },
    );
  }

  @override
  void dispose() {
    _scrollController.dispose();
    super.dispose();
  }

  void _onScroll() {
    if (_isBottom) _articleBloc.add(Fetch());
  }

  bool get _isBottom {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.offset;
    return currentScroll >= (maxScroll * 0.9);
  }
}

bug example good first issue

Most helpful comment

In infinite list example
I think the error is here:
Current:

  bool get _isBottom {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.offset;
    return currentScroll >= (maxScroll * 0.9);
  }

Fix:

  bool get _isBottom {
    if (_scrollController.hasClients) {
      final maxScroll = _scrollController.position.maxScrollExtent;
      final currentScroll = _scrollController.offset;
      return currentScroll >= (maxScroll * 0.9);
    }
    return false;
  }

All 5 comments

Hi @glin94 馃憢
Thanks for opening an issue!

Are you experiencing this issue when running the infinite list example? If not, can you please share a link to an app which reproduces the issue? Thanks 馃憤

I'm currently trying to run the infinite list example too and this error pops up

Hi @felangel , please assign this issue to me

In infinite list example
I think the error is here:
Current:

  bool get _isBottom {
    final maxScroll = _scrollController.position.maxScrollExtent;
    final currentScroll = _scrollController.offset;
    return currentScroll >= (maxScroll * 0.9);
  }

Fix:

  bool get _isBottom {
    if (_scrollController.hasClients) {
      final maxScroll = _scrollController.position.maxScrollExtent;
      final currentScroll = _scrollController.offset;
      return currentScroll >= (maxScroll * 0.9);
    }
    return false;
  }
Was this page helpful?
0 / 5 - 0 ratings