Skip to content

ScrollController attached to multiple scroll views. 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 111 pos 12: '_positions.length == 1' #70764

Closed
@haibowen

Description

@haibowen

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

pedromassangocode commented on Nov 19, 2020

@pedromassangocode

Hi @haibowen
Looks like you are using a too old version of Flutter.
Can you please upgrade to the latest stable and try again
flutter channel stable
flutter upgrade --force
flutter doctor -v

If the issue persists, please provide your flutter doctor -v.

added
in triagePresently being triaged by the triage team
waiting for customer responseThe Flutter team cannot make further progress on this issue until the original reporter responds
on Nov 19, 2020
haibowen

haibowen commented on Nov 19, 2020

@haibowen
Author

Hi @haibowen
Looks like you are using a too old version of Flutter.
Can you please upgrade to the latest stable and try again
flutter channel stable
flutter upgrade --force
flutter doctor -v

If the issue persists, please provide your flutter doctor -v.

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)

removed
waiting for customer responseThe Flutter team cannot make further progress on this issue until the original reporter responds
on Nov 19, 2020
pedromassangocode

pedromassangocode commented on Nov 20, 2020

@pedromassangocode

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

haibowen commented on Nov 20, 2020

@haibowen
Author

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

Hi @pedromassangocode

this is my code

 `import 'package:flutter/material.dart';

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) {

return Scaffold(
  appBar: AppBar(
    title: Text(widget.title),
  ),
  body: ListView.builder(
    itemBuilder: (BuildContext context, int index) {
      return ItemWidget();
    },
    itemCount: 30,
  ),

  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: [
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('稀释舒克舒克快速开始可快速开始'),
),
],
),
);
}
}
`

removed
waiting for customer responseThe Flutter team cannot make further progress on this issue until the original reporter responds
on Nov 20, 2020
pedromassangocode

pedromassangocode commented on Nov 23, 2020

@pedromassangocode

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

github-actions commented on Aug 9, 2021

@github-actions

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.

locked as resolved and limited conversation to collaborators on Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in triagePresently being triaged by the triage team

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @haibowen@pedromassangocode

        Issue actions

          ScrollController attached to multiple scroll views. 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 111 pos 12: '_positions.length == 1' · Issue #70764 · flutter/flutter