簡體   English   中英

Flutter 錯誤:“ScrollController 未附加到任何滾動視圖。” 在卷軸上

[英]Flutter error: 'ScrollController not attached to any scroll views.' on scroll

每當我在列表視圖中滾動時,我都會在控制台中收到此錯誤消息:

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

我一整天都在嘗試解決這個問題,我想讓其他人看看。 這段代碼還有更多問題,但現在我主要對修復這個錯誤感興趣。

我試過使用 Listview.builder,檢查 hController.hasClients 和更多小東西。 他們似乎沒有改變任何東西

class MyHome extends StatefulWidget {
  @override
  MyHomeState createState() => new MyHomeState();
}

class MyHomeState extends State<MyHome> with SingleTickerProviderStateMixin {
  ScrollController hController;
  ScrollController tController;
  ScrollController fController;
  ScrollController bController;

  @override
  void initState() {
    super.initState();
    hController = new ScrollController()..addListener(_scrollListener);
    tController = new ScrollController()..addListener(_scrollListener);
    fController = new ScrollController()..addListener(_scrollListener);
    bController = new ScrollController()..addListener(_scrollListener);
  }

  @override
  void dispose() {
    super.dispose();
    hController.removeListener(_scrollListener);
    tController.removeListener(_scrollListener);
    fController.removeListener(_scrollListener);
    bController.removeListener(_scrollListener);
  }
  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
        length: 4,
        child: new Scaffold(
          //Removed AppBar for readability
          body: new TabBarView(
            children: [
              new Container(// hot
                child: ListView(
                    controller: hController,
                    children: <Widget>[
                      Utils.show("hot")
                    ],
                ),
              ),
              new Container( //Trending
                child: ListView(
                  controller: tController,
                  children: <Widget>[
                    Utils.show("trending")
                  ],
                ),
              ),
              new Container( //Fresh
                child: ListView(
                  controller: fController,
                  children: <Widget>[
                    Utils.show("fresh")
                  ],
                ),
              ),
              new Container( //Best
                child: ListView(
                  controller: bController,
                  children: <Widget>[
                    Utils.show("best")
                  ],
                ),
              ),
            ],
          ),
        ));
  }
  void _scrollListener() {
    if (hController.position.extentAfter == 0.0) {
      setState(() {
        Utils.show("hot");
      });
    }else if (tController.position.extentAfter == 0.0) {
      setState(() {
        Utils.show("trending");
      });
    } else if (fController.position.extentAfter == 0.0) {
      setState(() {
        Utils.show("fresh");
      });
    } else if (bController.position.extentAfter == 0.0) {
      setState(() {
        Utils.show("best");
      });
    }
  }

}

編輯:為了清楚起見,我第一次發布這段代碼時,我使用了兩次 tController。 這當然是一個錯誤,但並沒有解決錯誤。 在四個列表視圖中的每一個中滾動時都會發生錯誤。

為避免此類錯誤,請使用 getter

ScrollController.hasClient

如果為 false,則不得調用與 [ScrollPosition] 交互的成員,例如 [position]、[offset]、[animateTo] 和 [jumpTo]。

例如:

    if (_controller.hasClients) {
      _controller.animateTo(
      ...
    }

問題在_scrollListener內部。

當您在if-else中檢查控制器時,場景中只有一個視圖。 意味着只有一個listview被渲染並且只有一個scrollcontroller被完全設置。 但是在您的代碼中,他們正在檢查單個 function 中所有滾動控制器的位置。 這就是您收到此錯誤的原因。 首先檢查 controller 是否有位置,只有在連接了 controller 並且視圖正確呈現時才會有位置。 之后檢查extentAfter值。

前 -

if (hController.positions.length > 0 && tController.position.extentAfter == 0.0) {
}
else if (tController.positions.length > 0 && tController.position.extentAfter == 0.0) {
}

& 很快

查看controller沒有client ant然后延時跳轉:

if (!_scrollController.hasClients) {
      _scrollController.animateTo(_scrollController.position.maxScrollExtent,
        duration: const Duration(milliseconds: 500),
        curve: Curves.easeInOut);
    }

如果您正確粘貼代碼 - 似乎可能有錯誤:

new Container(// hot
  child: ListView(
    controller: tController,
    children: <Widget>[
      Utils.show("hot")
    ],
  ),
),
new Container( //Trending
  child: ListView(
    controller: tController,
    children: <Widget>[
      Utils.show("trending")
    ],
  ),
),

你用過兩次tController沒用過hController

更新你的 flutter sdk 它將解決這個問題 這對我來說是工作在你的 cmd 上運行這個 - flutter update

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM