_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;
  }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章