Repository navigation
ScrollController attached to multiple scroll views Failed assertion: line 109 pos 12: '_positions.length == 1' #80664
Copy link
Copy link
Closed
Labels
r: invalidIssue is closed as not validIssue is closed as not valid
Description
My code
import 'package:flutter/material.dart';
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
PageController _pageController;
ScrollController _scrollController;
double offset = 0.0;
@override
void initState(){
super.initState();
_pageController = PageController(initialPage: 0);
_scrollController = ScrollController();
_scrollController.addListener(this.swapPageListener);
}
void swapPageListener() {
WidgetsBinding.instance.addPostFrameCallback((_){
offset = _scrollController.offset;
// _pageController.hasClients
if (offset > _scrollController.position.maxScrollExtent + 100) {
if(_pageController.hasClients) {
_pageController.animateToPage(1, duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
// _pageController.animateTo(offset, duration: duration, curve: curve)
}
}
if (offset < _scrollController.position.minScrollExtent - 100) {
_pageController.previousPage(duration: Duration(milliseconds: 500), curve: Curves.easeInOut);
}
});
}
// @override
// void dispose() {
// super.dispose();
// _pageController.dispose();
// }
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: PageView.builder(
controller: _pageController,
scrollDirection: Axis.vertical,
itemCount: 2,
physics: NeverScrollableScrollPhysics(),
itemBuilder: (context, idx) {
if(idx == 0) {
return _screen1();
} else {
return _screen2();
}
}
),
),
);
}
Widget _screen1() {
return SingleChildScrollView(
physics: BouncingScrollPhysics(),
controller: _scrollController,
child: Container(
height: 1000.0,
color: Colors.red,
child: Center(
child: Text('screen 1'),
),
),
);
}
Widget _screen2() {
return SingleChildScrollView(
physics: BouncingScrollPhysics(),
controller: _scrollController,
child: Container(
height: 1000.0,
color: Colors.green,
child: Center(
child: Text('screen 2'),
),
),
);
}
}
fluuter docker -v
[√] Flutter (Channel stable, 2.0.4, on Microsoft Windows [Version 10.0.19042.928], locale en-US)
• Flutter version 2.0.4 at C:\src\flutter
• Framework revision b1395592de (2 weeks ago), 2021-04-01 14:25:01 -0700
• Engine revision 2dce47073a
• Dart version 2.12.2
[!] Android toolchain - develop for Android devices (Android SDK version 30.0.1)
• Android SDK at C:\Users\Admin\AppData\Local\Android\Sdk
• Platform android-30, build-tools 30.0.1
• ANDROID_HOME = C:\Users\Admin\AppData\Local\Android\Sdk
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
X Android license status unknown.
Run `flutter doctor --android-licenses` to accept the SDK licenses.
See https://flutter.dev/docs/get-started/install/windows#android-setup for more details.
[√] Chrome - develop for the web
• Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe
[√] Android Studio (version 4.0)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 47.1.2
• Dart plugin version 193.7547
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01)
[√] VS Code (version 1.55.2)
• VS Code at C:\Users\Admin\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.21.0
[√] Connected device (3 available)
• Android SDK built for x86 64 (mobile) • emulator-5554 • android-x64 • Android 9 (API 28) (emulator)
• Chrome (web) • chrome • web-javascript • Google Chrome 87.0.4280.88
• Edge (web) • edge • web-javascript • Microsoft Edge 89.0.774.77
! Doctor found issues in 1 category.
I make error
ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 109 pos 12: '_positions.length == 1'
when use _pageController.animateToPage,
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
r: invalidIssue is closed as not validIssue is closed as not valid

Activity
Jahidul007 commentedon Apr 18, 2021
matamune94 commentedon Apr 18, 2021
It not working. I think setState is not the problem.
matamune94 commentedon Apr 18, 2021
Hello, I have found the solution to this problem.
iapicca commentedon Apr 18, 2021
@matamune94
I'm glad you found a solution,
but this issue doesn't seem to describe a bug or a feature request
the issue is likely to depend from you implementation
as you are clearly assigning the same controller to 2 different
ScrollViewif you need assistance with your code please follow the link
I want help writing my applicationin the new issue menuor ask on discord, reddit and slack
as a personal opinion, I recommend to use a state management for this type of situation
logic should not be handled in the widgets!
tiloc commentedon Apr 18, 2021
This might be similar to #74372
pedromassangocode commentedon Apr 19, 2021
As said above, this issue seems to be caused by your implementation as you are assigning the same controller into two different scroll views.
You may get some help if you post it on Stack Overflow and if you need help with your code, please see https://www.reddit.com/r/flutterhelp/
Closing, as this isn't an issue with Flutter itself. If you disagree, please write in the comments and I will reopen it.
Thank you
kevin4dhd commentedon Apr 24, 2021
Same problem, any solution without creating two controllers?
github-actions commentedon Aug 2, 2021
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.