Description
When
I used the component listview in flutter to nest the listview and set the scroll listener for the listview, the following error was reported: ScrollController attached to multiple scroll views.
'package:flutter/src/widgets/scroll_controller.dart':
Failed assertion: line 111 pos 12:'_positions.length == 1'
My code
`class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
ScrollController scrollController= new ScrollController();
void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
scrollController.addListener(() {
var index=scrollController.offset;
print("这个数是输出的$index");
});
}
@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
),
body: ListView.builder(itemBuilder: (BuildContext context,int index){
return ItemWidget();
},itemCount: 30,
controller: scrollController,
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
Widget ItemWidget(){
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
//color: Colors.pinkAccent,
),
height: 90,
width: MediaQuery.of(context).size.width,
child: Stack(
children: <Widget>[
Positioned(
top: 0,
left: 2,
child: Text('标题',style: TextStyle(color: Colors.pinkAccent),),
),
Positioned(
top: 15,
left: 2,
child: Container(
height: 45,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
controller: scrollController,
scrollDirection:Axis.horizontal,
itemCount: 10,itemBuilder: (BuildContext context,int index){
return Container(
height: 40,
width: 40,
margin: EdgeInsets.all(5),
color: Colors.blue,
);
}),
)
),
Positioned(
top: 70,
left: 5,
child: Text('稀释舒克舒克快速开始可快速开始'),
),
],
),
);
}
}`
My environment
[✓] Flutter (Channel stable, v1.17.3, on macOS 11.0.1 20B29, locale zh-Hans-CN)
• Flutter version 1.17.3 at /Users/hyplo/Downloads/flutter
• Framework revision b041144 (6 months ago), 2020-06-04 09:26:11 -0700
• Engine revision ee76268252
• Dart version 2.8.4
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
• Android SDK at /Users/hyplo/Library/Android/sdk
• Platform android-30, build-tools 30.0.0
• Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
• All Android licenses accepted.
[✓] Xcode - develop for iOS and macOS (Xcode 12.2)
• Xcode at /Applications/Xcode.app/Contents/Developer
• Xcode 12.2, Build version 12B45b
• CocoaPods version 1.9.3
[!] Android Studio (version 4.1)
• Android Studio at /Applications/Android Studio.app/Contents
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
[!] IntelliJ IDEA Community Edition (version 2020.2)
• IntelliJ at /Applications/IntelliJ IDEA CE.app
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
• For information about installing plugins, see
https://flutter.dev/intellij-setup/#installing-the-plugins
[✓] Connected device (1 available)
• iPhone 12 Pro Max • 04047B89-E391-4D7C-B35B-491A6C13AE66 • ios • com.apple.CoreSimulator.SimRuntime.iOS-14-2 (simulator)
Activity
pedromassangocode commentedon Nov 19, 2020
Hi @haibowen
Looks like you are using a too old version of Flutter.
Can you please upgrade to the latest
stable
and try againflutter channel stable
flutter upgrade --force
flutter doctor -v
If the issue persists, please provide your
flutter doctor -v
.haibowen commentedon Nov 19, 2020
Hi @pedromassangocode
I tried to follow the above steps and the problem still exists
My environment
Running flutter doctor...
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 1.22.4, on macOS 11.0.1 20B29 darwin-x64, locale zh-Hans-CN)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.0)
[✓] Xcode - develop for iOS and macOS (Xcode 12.2)
[!] Android Studio (version 4.1)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[!] IntelliJ IDEA Community Edition (version 2020.2)
✗ Flutter plugin not installed; this adds Flutter specific functionality.
✗ Dart plugin not installed; this adds Dart specific functionality.
[✓] Connected device (1 available)
pedromassangocode commentedon Nov 20, 2020
Hi @haibowen
Can you please edit the code you provide to make it complete? I should be able to reproduce the issue by just copy/pasting the code above.
Thank you
haibowen commentedon Nov 20, 2020
Hi @pedromassangocode
this is my code
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@OverRide
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@OverRide
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State {
int _counter = 0;
ScrollController scrollController = new ScrollController();
void _incrementCounter() {
setState(() {
_counter++;
});
}
@OverRide
void initState() {
// TODO: implement initState
super.initState();
scrollController.addListener(() {
var index = scrollController.offset;
print("这个数是输出的$index");
});
}
@OverRide
Widget build(BuildContext context) {
}
Widget ItemWidget() {
return Container(
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(5),
//color: Colors.pinkAccent,
),
height: 90,
width: MediaQuery.of(context).size.width,
child: Stack(
children: [
Positioned(
top: 0,
left: 2,
child: Text(
'标题',
style: TextStyle(color: Colors.pinkAccent),
),
),
Positioned(
top: 15,
left: 2,
child: Container(
height: 45,
width: MediaQuery.of(context).size.width,
child: ListView.builder(
controller: scrollController,
scrollDirection: Axis.horizontal,
itemCount: 10,
itemBuilder: (BuildContext context, int index) {
return Container(
height: 40,
width: 40,
margin: EdgeInsets.all(5),
color: Colors.blue,
);
}),
)),
Positioned(
top: 70,
left: 5,
child: Text('稀释舒克舒克快速开始可快速开始'),
),
],
),
);
}
}
`
pedromassangocode commentedon Nov 23, 2020
Hi @haibowen
This error is expected because you are using the same scroll controller for multiple ListViews and this is not accepted. Make sure each Item has its own controller.
github-actions commentedon Aug 9, 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 -v
and a minimal reproduction of the issue.