0

I'm encountering an error in my Flutter application related to a ScrollController. I have a QuestionDetailsScreen where I'm using a ScrollController to monitor the scroll position. I want to trigger a function in my controller fireWhenCloseQuestionDetails() when the QuestionDetailsScreen route is closed. To do this, I've implemented a custom NavigatorObserver. However, I'm getting an error:

'_positions.isNotEmpty': ScrollController not attached to any scroll views

Here's my QuestionBankController:

  class QuestionBankController extends GetxController {
  ScrollController questionDetailsScrollCtrl = ScrollController();

  @override
  void onInit() {
    questionDetailsScrollCtrl = ScrollController();
    questionDetailsScrollCtrl.addListener(_onScroll);
    super.onInit();
  }

  void _onScroll() {
    // This function will be called every time the scroll position changes
    print("Scroll position: ${questionDetailsScrollCtrl.position.pixels}");
  }

  void fireWhenCloseQuestionDetails() {
    print("Fire");
    print(questionDetailsScrollCtrl.position.pixels);
  }

  @override
  void onClose() {
    print("On close");
    questionDetailsScrollCtrl.dispose();
    super.onClose();
  }
}

I'm using a custom NavigatorObserver to trigger fireWhenCloseQuestionDetails() when the QuestionDetailsScreen route is closed:

class MyNavigatorObserver extends NavigatorObserver {
  @override
  void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
    if (route.settings.name == "/questiondetailsscreen") {
      Future.delayed(Duration.zero, () {
        QuestionBankController().fireWhenCloseQuestionDetails();
      });
    }
  }
}

It seems like the issue might be related to creating a new instance of QuestionBankController when calling fireWhenCloseQuestionDetails(). How can I ensure I'm using the correct instance of QuestionBankController associated with my QuestionDetailsScreen specifically when closing the route?

Additional Information:

  • The scroll listening functionality is working correctly while scrolling.

1 Answer 1

0

How about calling fireWhenCloseQuestionDetails() in QuestionBankController's onClose?

@override
void onClose() {
  print("On close");
  fireWhenCloseQuestionDetails();
  questionDetailsScrollCtrl.dispose();
  super.onClose();
}

Probably if your QuestionBankController is bound to QuestionDetailsScreen.

Sign up to request clarification or add additional context in comments.

4 Comments

actually, I use this controller for two pages. that's mean when I get back from QuestionDetailsScreen() to QuestionListScreen() then my controller exist
I tried also this, but not working
Then change QuestionDetailsScreen to StatefulWidget, and override onDispose to call fireWhenCloseQuestionDetails.
still not working

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.