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

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

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

    Issue actions