I have a ScrollController attached to a Listview, but when i scroll it throws an Exception:
@override
initState() {
super.initState();
_mainCategoriesScrollController = ScrollController();
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
_mainCategoriesScrollController
.addListener(_mainCatergoriesScrollListener());
});
}
_mainCatergoriesScrollListener() {
if (_mainCategoriesScrollController.offset >=
_mainCategoriesScrollController.position.maxScrollExtent &&
!_mainCategoriesScrollController.position.outOfRange) {
print("reach the bottom");
}
if (_mainCategoriesScrollController.offset <=
_mainCategoriesScrollController.position.minScrollExtent &&
!_mainCategoriesScrollController.position.outOfRange) {
print("reach the top");
}
}
and the build method
@override
Widget build(BuildContext context) {
_setCurrentMainCategory();
SystemChrome.setEnabledSystemUIOverlays([]);
return Container(
child: Column /*or Column*/ (
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Expanded(
flex: 9,
child: Container(
child: Row(
children: <Widget>[
Expanded(
flex: 3,
child: ListView.builder(
controller: _mainCategoriesScrollController,
shrinkWrap: true,
itemCount: _mainCategories.length,
itemBuilder: (context, index) {
return MainCategoryEntry(
mainCategory: _mainCategories[index],
mainCategorySelected: () {
MyApp.setActivity(context);
setState(() {
currentMainCategory =
_mainCategories[index];
});
},
isSelected: _mainCategories[index] ==
currentMainCategory,
);
}),
),
The exception is the follwing:
════════ Exception caught by foundation library ════════════════════════════════ The following NoSuchMethodError was thrown while dispatching notifications for ScrollController: The method 'call' was called on null. Receiver: null Tried calling: call()
When the exception was thrown, this was the stack
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5)
#1 ChangeNotifier.notifyListeners
package:flutter/…/foundation/change_notifier.dart:207
#2 ChangeNotifier.notifyListeners
package:flutter/…/foundation/change_notifier.dart:207
#3 ScrollPosition.notifyListeners
package:flutter/…/widgets/scroll_position.dart:775
#4 ScrollPosition.setPixels
package:flutter/…/widgets/scroll_position.dart:244
...
The ScrollController sending notification was: ScrollController#7153b(one client, offset 0.7)
════════════════════════════════════════════════════════════════════════════════
Can someone tell me what im doing wrong? Many thanks!