_positions.isNotEmpty: ScrollController not attached to any scroll views.

Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 110 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

这个问题试了很多方法

终于在这个网站https://stackoverflow.com/questions/52296387/scrollcontroller-jumpto-scrollcontroller-not-attached-to-any-scroll-views看到解决的要点

问题是出在

1

2

3

4

5

  if (_scrollController.position.pixels == 0 && prePixels != 0) {
    _scrollController.jumpTo(prePixels);
  }
  prePixels = _scrollController.position.pixels;

 我使用了上面的代码,(向上滑动后这部分UI不显示了,所以flutter就不渲染了,_scrollController也就不存在了),所以报错了。

解决方案是加一个判断,等向上滑动后UI不显示了,就不执行这部分代码就好了

上面那个网站给出了是 if (_scrollController.hasClients){}

增加后

  if (_scrollController.hasClients) {
    if (_scrollController.position.pixels == 0 && prePixels != 0) {
      _scrollController.jumpTo(prePixels);
    }
    prePixels = _scrollController.position.pixels;
  }

你可能感兴趣的:(flutter)