【问题标题】:Flutter Error "PageController.page cannot be accessed before a PageView is built with it."Flutter 错误“在使用它构建 PageView 之前无法访问 PageController.page。”
【发布时间】:2021-06-11 20:43:29
【问题描述】:

在 PageViewBuilder 中,我想使用我在 pub.dev 上找到的 ArgonTimerButton 上的 onTap 移动到下一页,而不是滑动手势。我创建了一个控制器并使用了 controller.nextPage() 但它给了我这个错误:

'positions.isNotEmpty': PageController.page cannot be accessed before a PageView is built with it.

我的代码:

 final controller = PageController();
   @override
   Widget build(BuildContext context) {
   MediaQueryData size = MediaQuery.of(context).size;
   return  PageView.builder(
      physics: NeverScrollableScrollPhysics(),
      controller: PageController(),
      itemCount: widget.workoutExcercises.length,
      itemBuilder: (context, index) {
        return 

              //other widgets

              ArgonTimerButton(
                initialTimer: 3, // Optional
                height: 50,
                width: size.width * 0.55,
                minWidth: width * 0.40,
                color: Colors.white,
                borderRadius: 5.0,
                curve: Curves.easeInToLinear,
                child: Text(
                  "Done?",
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 24,
                      fontWeight: FontWeight.w700),
                ),
                loader: (timeLeft) {
                  return Text(
                    "Rest Time | $timeLeft",
                    style: TextStyle(
                        color: Colors.black,
                        fontSize: 18,
                        fontWeight: FontWeight.w700),
                  );
                },
                onTap: (startTimer, btnState) {
                  if (btnState == ButtonState.Idle) {
                    controller.nextPage(duration: Duration(milliseconds:1000), curve: Curves.easeInToLinear);
                  }
                },
              ),
            ],
          ),
        );
      },
    );

我该如何解决这个问题?

【问题讨论】:

    标签: android flutter dart-pub


    【解决方案1】:

    您正在创建控制器,但您没有使用它。

    而不是:

    controller: PageController(),
    

    做:

    controller: controller,
    

    【讨论】:

      猜你喜欢
      • 2021-03-25
      • 1970-01-01
      • 2020-07-18
      • 2019-05-23
      • 1970-01-01
      • 2016-04-11
      • 2011-04-07
      • 1970-01-01
      相关资源
      最近更新 更多