Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Loading callbacks not firing when inside FutureBuilder #17

Closed
zmeggyesi opened this issue Jan 23, 2020 · 5 comments
Closed

Loading callbacks not firing when inside FutureBuilder #17

zmeggyesi opened this issue Jan 23, 2020 · 5 comments

Comments

@zmeggyesi
Copy link

I have a somewhat weird issue around loading indicators and this widget. In one part of my app, I load a rather sizable list from the server, so I display a loading indicator using a FutureBuilder<http.Response>, so that the app shows something while the fetch is underway, then switch to this ListView once I have the data. This is the code in question:

FutureBuilder<http.Response>(
  future: _initialGet, // set to a Future in the `initState()` method
  builder: (BuildContext context, AsyncSnapshot<http.Response> snapshot) {
    if (snapshot.hasData) {
      http.Response response = snapshot.data;
      print(response.statusCode);
      SearchResults results = SearchResults.fromBuffer(response.bodyBytes);
      _cursor = results.cursor;
      _hasMore = results.hasMore;
      _updateList(results.sites); // this method updates the `_items` list used below

      return Container(
        key: Key("generalList"),
        child: IncrementallyLoadingListView(
          key: ValueKey(_items),
          shrinkWrap: false,
          itemCount: () => _items.length,
          hasMore: () => _hasMore,
          loadMoreOffsetFromBottom: 5,
          itemBuilder: (context, index) {
            // create tile
          },
          loadMore: () async {
            // load more stuff
          },
        ),
      );
    } else if (snapshot.hasError) {
      // Only for debug and testing really
      return Container(
        key: Key("generalList"),
        child: Text("${snapshot.data.statusCode}"),
      );
    } else {
      // Loading indicator
      return Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(
            child: CircularProgressIndicator(),
            width: 60,
            height: 60,
          ),
          const Padding(
            padding: EdgeInsets.only(top: 16),
            child: Text('Loading...'),
          )
        ],
      );
    }
  },
)

The loading indicator appears and disappears, the list is created, and has all the items I loaded. However, the callbacks, namely loadMore don't seem to fire when the end of the list is reached - a breakpoint set in that function is never hit. The same does not happen when outside the FutureBuilder.

Is it possible the FutureBuilder is somehow interfering with detecting the end of the list?

@MaikuB
Copy link
Owner

MaikuB commented Jan 23, 2020

There's an example app in the repo that has a FutureBuilder as well and doesn't exhibit the same problem you described so I wouldn't think so. There's a difference though in that it checks the connection state. Perhaps it'll help in your case

@zmeggyesi
Copy link
Author

Let me take look at it, thanks for pointing it out.

@MaikuB
Copy link
Owner

MaikuB commented Jan 23, 2020

It's likely that you'll need to look at your code that may need restructuring. Sounds like somehow hasMore evaluates to false. The code is open source so you could pull it into your project to help debug

@zmeggyesi
Copy link
Author

I'll need to take a look at that too, somehow. Changing the pattern didn't really help, so it's on to debugging hasMore...

@zmeggyesi
Copy link
Author

Thanks for the fresh pair of eyes, MaikuB! I managed to track down the problem, and it was all my fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants