Issue in chapter5 challenge 1 in flutter apprentice book

Hello Everyone.
I was working on the challenge given in chapter 5 in the book , when you reach the bottom you should log a message that you are at the bottom and vis versa , the problem here is that after trying my own code and it didn’t work I copied the exact code in the book and didn’t work also so what I did is I asked chatGpt to help me fixing my bug and he gave me this :
It looks like you are trying to use a ScrollController to detect when the user has reached the top or bottom of the list. There are a few issues with the code you have provided:

  1. The outOfRange property of the ScrollPosition is always false. This means that the condition _scrollController.position.outOfRange will never be true, so the log messages will never be printed.
  2. You are using the maxScrollExtent and minScrollExtent properties to determine if the user has reached the top or bottom of the list, but these values only represent the maximum and minimum scroll positions of the content, not the visible area of the list.

To detect when the user has reached the top or bottom of the list, you can use the extentBefore and extentAfter properties of the ScrollPosition instead. These values represent the amount of content that is visible before and after the current scroll position.

Here is an example of how you could modify your code to log a message when the user has reached the top or bottom of the list:

And here is the code that he gave me :

void _scrollListener() {
  if (_scrollController.position.extentBefore == 0) {
    log('Reached the top');
  }

  if (_scrollController.position.extentAfter == 0) {
    log('Reached the bottom');
  }
}

Should the code there be updated ?

Hi @amoxe thank you, this needs to be updated!

1 Like