我有一个ScrollController连接到一个Listview,但当我滚动时它会抛出异常:
@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");
}
}
以及构建方法
@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,
);
}),
),
以下是异常情况:
════════ 基础库捕获的异常 ════════════════════════════════ 在 ScrollController 的通知分发期间发现了 NoSuchMethodError 异常: 调用 null 上的方法 'call'。 接收器:null 尝试调用: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)
════════════════════════════════════════════════════════════════════════════════
有人能告诉我我做错了什么吗?非常感谢!