gpt4 book ai didi

Flutter ScrollController 抛出异常

转载 作者:行者123 更新时间:2023-12-04 15:26:23 25 4
gpt4 key购买 nike

我有一个附加到 Listview 的 ScrollController,但是当我滚动时它会抛出异常:

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

异常(exception)情况如下:

════════基础库捕获异常════════════════════════════════ 在为 ScrollController 调度通知时抛出以下 NoSuchMethodError: 在 null 上调用了方法“call”。 接收者:空 尝试调用: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)
════════════════════════════════════════════════════════════════════════════════

谁能告诉我我做错了什么?非常感谢!

最佳答案

这一行似乎调用了该​​方法,而不是向监听器添加 _mainCategoriesScrollListener 方法:

_mainCategoriesScrollController.addListener(_mainCatergoriesScrollListener());

我猜错误是在您甚至未分配客户端时尝试访问 _mainCategoriesScrollController 方法时发生的。

即使您尝试添加帧后回调,方法 _mainCatergoriesScrollListener() 仍会在您调用它时在 initState() 中被调用,而不是将其添加到监听器。

这可能是错误的原因。

尝试将其更改为:

_mainCategoriesScrollController.addListener(_mainCatergoriesScrollListener);

_mainCategoriesScrollController.addListener(() => _mainCatergoriesScrollListener());

关于Flutter ScrollController 抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62165939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com