Flutter ScrollController is throwing exception

Viewed 1463

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!

1 Answers

Instead of adding _mainCategoriesScrollListener method to the listener, this line seems to call the method:

_mainCategoriesScrollController.addListener(_mainCatergoriesScrollListener());

I guess the error occurs when you try to access _mainCategoriesScrollController methods when the client isn't even assigned.

Even when you try to add a post-frame callback, the method _mainCatergoriesScrollListener() still gets called in initState() as you are calling it instead of adding it to the listener.

This might be the cause of the error.

Try changing it to:

_mainCategoriesScrollController.addListener(_mainCatergoriesScrollListener);

or

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