'How to solve '_positions.isNotEmpty': ScrollController not attached to any scroll views?
I have an object fixedTest that looks like a list of dictionaries, and i am taking data from it to create a scrollviewobject. The original ListWheelScrollView doesn't capture taps, so i'm using the custom Widget from package clickable_list_wheel_view. When I'm trying to launch the test code:
body: ClickableListWheelScrollView(
scrollController: _scrollController,
itemHeight: 100,
itemCount: 100,
onItemTapCallback: (index) {
print("onItemTapCallback index: $index");
},`
Error:
[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: 'package:flutter/src/widgets/scroll_controller.dart': Failed assertion: line 108 pos 12: '_positions.isNotEmpty': ScrollController not attached to any scroll views.
Code:
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),
),
],
),
)
],
),
))),
))
I can't understand what's the problem. I found about addListener(), but I misunderstand how to use it.
Solution 1:[1]
Use the scroll controller after you have build the widget tree
SchedulerBinding.instance.addPostFrameCallback((_) {
if (scrollController.hasClients) {
//do your stuff here
}
});
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | SAM SHELDIN THOMAS |
