2

I am getting an error in scroll view controller. Here is error:

flutter: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 149 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.

I used following code and I hope due to this it is generating:

return ListView(
  controller: _mealScrollController,
  children: <Widget>[
    Container(
      height: screenSize.height * 0.35,
    ),

    WorkWidget(workTime: workTime.morning),
    WorkWidget(workTime: workTime.morning),
    WorkWidget(workTime: workTime.noon),
    WorkWidget(workTime: workTime.evening),
    WorkWidget(workTime: workTime.lateevening),
    WorkWidget(workTime: workTime.night),
    WorkWidget(workTime: workTime.midnight),
    Container(
      child: Center(
        child: RotateWidget(
          child: Icon(
            Icons.refresh,
            size: AppSize.medium,
          ),
          onTap: widget.callBack,
        ),
      ),
    ),
  ],
);
3
  • Please post a minimal reproduction of your code. I found only github.com/flutter/flutter/issues/21683 that matches the error message. Commented Mar 8, 2019 at 6:45
  • I have added code. Commented Mar 8, 2019 at 6:51
  • It has to be some place in your code, where you call one of methods of _mealScrollController - jumpTo or animateTo, or try to get position value. Could you post this part of code? Commented Sep 27, 2019 at 13:00

1 Answer 1

0

Judging from the logs you've given, it seems that the error came from scroll_controller.dart and wasn't caused by List<Widget>inside ListView.

I can only replicate this behavior if ScrollController is called before being initialized in build. If you can provide a complete minimal repro on how you're using ScrollController, we can take a look at it.

Here's a sample that can replicate the behavior.

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,
      ),
      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<MyHomePage> {
  var _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    // Calling _scrollController first without being initialized in the build
    // will cause this error: "ScrollController not attached to any scroll views."
    debugPrint('This will cause an error ${_scrollController.position.atEdge}');

    // I suggest using a listener if you're trying to 
    // trigger functions on scroll change
    _scrollController.addListener(() {
      if (_scrollController.position.atEdge) {
        if (_scrollController.position.pixels == 0)
          print('List scroll at top');
        else {
          print('List scroll at bottom');
        }
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: LayoutBuilder(builder: (context, constraints) {
        return ListView.builder(
          controller: _scrollController,
          itemCount: 10,
          itemBuilder: (context, index) {
            return Container(
                height: constraints.maxHeight * 0.2,
                color: index % 2 == 0 ? Colors.blueAccent : Colors.redAccent);
          },
        );
      }),
    );
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.