颤振错误 "PageController.page cannot be accessed before a PageView is built with it."
Flutter Error "PageController.page cannot be accessed before a PageView is built with it."
在 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);
}
},
),
],
),
);
},
);
我该如何解决?
您正在创建控制器但并未使用它。
而不是:
controller: PageController(),
做:
controller: controller,
在 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);
}
},
),
],
),
);
},
);
我该如何解决?
您正在创建控制器但并未使用它。
而不是:
controller: PageController(),
做:
controller: controller,