snowballdigital / flutter-graphql

A GraphQL client for Flutter, bringing all the features from a modern GraphQL client to one easy to use package. Built after react apollo
MIT License
47 stars 7 forks source link

Loading boolean only set true on initial load #6

Closed Smidds closed 5 years ago

Smidds commented 5 years ago

Describe the bug The loading boolean is only ever true, once. Meaning, if you were to wrap the Query in a StatefulWidget, or a ScopedModelDescendant as in my case, the build method causes the Query to fire off its builder, but the result.loading is never actually set to true to indicate that the Query is loading again.

To Reproduce Steps to reproduce the behavior:

  1. Construct a StatefulWidget or ScopedModelDescedant, or anything that will fire off a rebuild
  2. Set a Query widget as a child of your StatefulWidget
  3. Create an if statement within the builder for Query to print out "Loading!" when result.loading is true
  4. Add a button or some other trigger that will change the state of the Queries parent, firing the parent's build method
  5. Load the application
  6. Notice that "Loading!" fires on first application load
  7. Press the button / fire the trigger
  8. Notice that "Loading!" does not appear in the log, indicating that result.loading is never reset to true

Expected behavior This might be my misunderstanding when it comes to the loading boolean, but I anticipated that loading would be set to true whenever the Query is fetching data, and especially when the whole widget is rebuilding.

Screenshots n/a

Desktop (please complete the following information):

Smartphone (please complete the following information):

Additional context I've done some digging around in the query.dart file and I've found that everything appears to initialize as expected when the dependencies change, and the Query widget's internal build method does in fact fire every time we run its StatefulWidget parent's build method, which does in fact set initialData: QueryResult { loading: true } (line 88). My best guess is that this has something to do with not fully closing the stream, or perhaps something to do with the controller? No idea!

Smidds commented 5 years ago

I was able to work around this by setting the following:

void onListen() {
  if (options.fetchResults) {
    controller.add(
      QueryResult(
        loading: true,
      ),
    );
    fetchResults();
  }
}

All I've done perform controller.add at line 53 in observable_query.dart to set loading to true, and now the loading boolean is true when we rebuild the Query widget!

juicycleff commented 5 years ago

Sweet. I think we just need to get common functionalities running