'_positions.isNotEmpty': ScrollController not attached to any scroll views

别来无恙 提交于 2020-12-06 07:35:07

问题


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,
        ),
      ),
    ),
  ],
);

回答1:


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);
          },
        );
      }),
    );
  }
}


来源:https://stackoverflow.com/questions/55058031/positions-isnotempty-scrollcontroller-not-attached-to-any-scroll-views

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!