首页 > 解决方案 > 如何解决'_positions.isNotEmpty':ScrollController 未附加到任何滚动视图?

问题描述

我有一个看起来像字典列表的对象 fixedTest,我从中获取数据以创建一个滚动视图对象。原始的 ListWheelScrollView 不捕获点击,所以我使用来自包clickable_list_wheel_view的自定义小部件。当我尝试启动测试代码时:

body: ClickableListWheelScrollView(
      scrollController: _scrollController,
      itemHeight: 100,
      itemCount: 100,
      onItemTapCallback: (index) {
        print("onItemTapCallback index: $index");
      },`

错误:

[错误:flutter/lib/ui/ui_dart_state.cc(186)] 未处理的异常:'package:flutter/src/widgets/scroll_controller.dart':断言失败:第 108 行 pos 12:'_positions.isNotEmpty':未附加 ScrollController到任何滚动视图。

代码:

    launchURL(String url) async {
    if (await canLaunch(url)) {
      await launch(url, forceWebView: true);
    } else {
      throw 'Could not launch $url';
    }
  }

  @override
  Widget build (BuildContext context) {
    test = (ModalRoute.of(context).settings.arguments);
    var fixedTest = test['data'].news4today;
    final _scrollController = ScrollController();
    checkUrls() {
      for (int i = 0; i < fixedTest.length; i++) {
        if (fixedTest[i]['urlToImage'] == null ||
            !isURL(fixedTest[i]['urlToImage'])) {
          fixedTest[i]['urlToImage'] =
              'https://i.pinimg.com/originals/8a/eb/d8/8aebd875fbddd22bf3971c3a7159bdc7.png';
        }
        if (fixedTest[i]['url'] == null || !isURL(fixedTest[i]['url'])) {
          fixedTest[i]['url'] = 'https://google.com';
        }
      }
    }

    checkUrls();

    return Scaffold(
        backgroundColor: Colors.white,
        appBar: AppBar(
          title: Center(child: Text('News')),
          backgroundColor: Colors.deepPurpleAccent,
        ),
        body: ClickableListWheelScrollView(
          scrollController: _scrollController,
          itemHeight: 100,
          itemCount: 100,
          onItemTapCallback: (index) {
            print("onItemTapCallback index: $index");
          },
          child: ListWheelScrollView.useDelegate(
              itemExtent: 400,
              diameterRatio: 9,
              squeeze: 1.2,
              physics: BouncingScrollPhysics(),
              onSelectedItemChanged: (index) {
                // debugPrint(index.toString());
              },
              childDelegate: ListWheelChildBuilderDelegate(
                  childCount: 100,
                  builder: (context, index) => Container(
                        child: Stack(
                          alignment: Alignment.topCenter,
                          children: <Widget>[
                            Container(
                              height: 353.0,
                              decoration: BoxDecoration(
                                image: DecorationImage(
                                    fit: BoxFit.scaleDown,
                                    alignment: FractionalOffset.center,
                                    image: NetworkImage(
                                        fixedTest[index]['urlToImage'])),
                              ),
                            ),
                            Container(
                              margin: EdgeInsets.only(top: 285),
                              child: Padding(
                                padding: const EdgeInsets.all(10),
                                child: SizedBox(
                                  // child: new RichText(
                                  //   text: new TextSpan(text: 'Non touchable. ', children: [
                                  //     new TextSpan(
                                  //       text: 'Tap here.',
                                  //       recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
                                  //     )
                                  //   ]),
                                  // )


                                    child: Text(
                                  fixedTest[index]['title'],
                                  style: TextStyle(fontSize: 16),
                                  maxLines: 4,
                                  textAlign: TextAlign.center,
                                )
                                ),
                              ),
                              decoration: BoxDecoration(
                                color: Colors.purple[100],
                                boxShadow: [
                                  BoxShadow(
                                    color: Colors.grey.withOpacity(0.8),
                                    spreadRadius: 1,
                                    blurRadius: 3,
                                    offset: Offset(1, 1),
                                  ),
                                ],
                              ),
                            )
                          ],
                        ),
                      ))),
        ))

我不明白有什么问题。我发现了关于 addListener(),但我误解了如何使用它。

标签: flutterflutter-layoutflutter-dependencies

解决方案


问题是你没有添加滚动控制器ListWheelScrollView.useDelegate,所以它返回滚动控制器为空的错误。

return Scaffold(
    backgroundColor: Colors.white,
    appBar: AppBar(
      title: Center(child: Text('News')),
      backgroundColor: Colors.deepPurpleAccent,
    ),
    body: ClickableListWheelScrollView(
      scrollController: _scrollController,
      itemHeight: 100,
      itemCount: 100,
      onItemTapCallback: (index) {
        print("onItemTapCallback index: $index");
      },
      child: ListWheelScrollView.useDelegate(
          controller: _scrollController,
          itemExtent: 400,
          diameterRatio: 9,
          squeeze: 1.2,
          physics: BouncingScrollPhysics(),
          onSelectedItemChanged: (index) {
            // debugPrint(index.toString());
          },
          childDelegate: ListWheelChildBuilderDelegate(
              childCount: 100,
              builder: (context, index) => Container(
                    child: Stack(
                      alignment: Alignment.topCenter,
                      children: <Widget>[
                        Container(
                          height: 353.0,
                          // decoration: BoxDecoration(
                          //   image: DecorationImage(
                          //     fit: BoxFit.scaleDown,
                          //     alignment: FractionalOffset.center,
                          //     image: NetworkImage(
                          //         fixedTest[index]['urlToImage'])
                          //   ),
                          // ),
                        ),
                        Container(
                          margin: EdgeInsets.only(top: 285),
                          child: Padding(
                            padding: const EdgeInsets.all(10),
                            child: SizedBox(
                                // child: new RichText(
                                //   text: new TextSpan(text: 'Non touchable. ', children: [
                                //     new TextSpan(
                                //       text: 'Tap here.',
                                //       recognizer: new TapGestureRecognizer()..onTap = () => print('Tap Here onTap'),
                                //     )
                                //   ]),
                                // )

                                child: Text(
                              "fixedTest[index]['title']",
                              style: TextStyle(fontSize: 16),
                              maxLines: 4,
                              textAlign: TextAlign.center,
                            )),
                          ),
                          decoration: BoxDecoration(
                            color: Colors.purple[100],
                            boxShadow: [
                              BoxShadow(
                                color: Colors.grey.withOpacity(0.8),
                                spreadRadius: 1,
                                blurRadius: 3,
                                offset: Offset(1, 1),
                              ),
                            ],
                          ),
                        )
                      ],
                    ),
                  ))),
    ));

推荐阅读