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

asData?.value should be always mapped to null when loading #2850

Closed
23doors opened this issue Aug 27, 2023 · 4 comments
Closed

asData?.value should be always mapped to null when loading #2850

23doors opened this issue Aug 27, 2023 · 4 comments
Assignees
Labels
bug Something isn't working needs triage

Comments

@23doors
Copy link

23doors commented Aug 27, 2023

Describe the bug

provider.asData?.value is returning previous value even if isLoading is true.

To Reproduce

// declare some basic provider (reproduced with Family provider but I guess it should occur without Family as well)
@riverpod
Future<int?> someData(SomeDataRef ref, {required int something}) async {
    await Future.delayed(Duration(seconds: 10));
    return something;
}

...
// in build:

final data = ref.watch(someDataProvider(0));
print("${data.isLoading} ${data.asData?.isLoading} ${data.asData?.value != null}");

...
// invalidate provider somewhere else
ref.invalidate(someDataProvider(0));

Once value is there, after invalidate isLoading will be = true, but data.asData?.value will still be holding a value (it will not map it to null as expected). So before new value is loaded you get true true true as output (which in theory should never happen).

Even with .map you get:

    data.map(
      data: (v) {
        print("hasData and yet isLoading=${data.isLoading}");
        return null;
      },
      error: (v) {
        print("hasError");
        return null;
      },
      loading: (v) {
        print("isLoading");
        return null;
      },
    );
flutter: hasData and yet isLoading=true
flutter: hasData and yet isLoading=false

loading is never fired in this case.

Expected behavior

When isLoading = true, asData?.value should be null (which is how docs describe it as well).

@23doors 23doors added bug Something isn't working needs triage labels Aug 27, 2023
@rrousselGit
Copy link
Owner

That's the expected behavior of AsyncData.
During a refresh, an AsyncData with isLoading=true is returned.

What you can do is asyncValue.unwrapPrevious().value

@23doors
Copy link
Author

23doors commented Aug 27, 2023

  /// If you do not want to return previous value during loading/error states,
  /// consider using [asData] :
  ///
  /// ```dart
  /// ref.watch(provider).asData()?.valueOrNull;
  /// ```
  /// Upcast [AsyncValue] into an [AsyncData], or return null if the [AsyncValue]
  /// is in loading/error state.

If that's expected behavior, then it is completely different from what docs describe it to be.

rrousselGit added a commit that referenced this issue Aug 27, 2023
@rrousselGit
Copy link
Owner

Good call, I've fixed this on master.

@AhmedLSayed9
Copy link
Contributor

@rrousselGit valueOrNull docs should be updated too

/// ref.watch(provider).asData()?.valueOrNull;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working needs triage
Projects
None yet
Development

No branches or pull requests

3 participants