'_positions.isNotEmpty':ScrollController未附加到任何滚动视图

时间:2019-03-08 06:44:00

标签: dart flutter

我在滚动视图控制器中遇到错误。这是错误:

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

我使用了以下代码,并且希望由此生成:

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 个答案:

答案 0 :(得分:0)

从您提供的日志来看,错误似乎是由scroll_controller.dart引起的,而不是由ListView内部的List<Widget>引起的。

如果在内部初始化之前调用ScrollController,我只能复制此行为。如果您可以提供有关如何使用ScrollController的完整的最小复制,我们可以看看它。

这是一个可以复制行为的示例。

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