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

Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt occurs under certain conditions #815

Closed
mono0926 opened this issue Oct 13, 2021 · 21 comments
Labels
bug Something isn't working

Comments

@mono0926
Copy link
Sponsor Contributor

mono0926 commented Oct 13, 2021

Describe the bug

After 293cc06 was released, Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt occurs under certain conditions

To Reproduce

It reproduced on our internal project, and I haven't been able to create a simple reproducible sample code yet 🙏

I observed this:

ref.watch(fooProvider);
ref.watch(barProvider); // The error occurs under certain conditions.

Expected behavior

Works fine without any errors.

@mono0926 mono0926 added bug Something isn't working needs triage labels Oct 13, 2021
@rrousselGit rrousselGit added question Further information is requested and removed needs triage labels Oct 13, 2021
@rrousselGit
Copy link
Owner

Hello!

Without a way to reproduce this, I have no means to K ow if that error is expected or not

@mono0926
Copy link
Sponsor Contributor Author

I'm sorry, but I'd like to add a note when I can make a reproducible code 🙏

@Arenukvern
Copy link

Faced the same issue. Last working version for me is 1.0.0-dev.7

@rrousselGit
Copy link
Owner

This error was added on purpose recently.

Please add a way to reproduce the issue, otherwise chances are this is expected.

@rrousselGit
Copy link
Owner

For information: This happens typically happens if you're using ref.something on a provider that was disposed.

It could be that you're using a FutureProvider like so:

final provider = FutureProvider((ref) {
  ref.watch(foo);
  await something();
  ref.watch(bar);
})

But during the await something, the provider was disposed (because foo changed), causing ref.watch(bar) to fail with this error

@KoheiKanagu
Copy link
Contributor

I had the same problem.

I've implemented a minimum of reproducible code, can you check it please?
https://gist.github.com/KoheiKanagu/d763d9d11f9204b554c6245b4c7c51a2

hooks_riverpod: 1.0.0-dev.11
❯ flutter --version
Flutter 2.5.3 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 18116933e7 (11 days ago) • 2021-10-15 10:46:35 -0700
Engine • revision d3ea636dc5
Tools • Dart 2.14.4
ControlRoom-2021-10-26-19-45-53.mp4

@rrousselGit
Copy link
Owner

Thanks for the reproducible code

Interestingly:

  • it doesn't happen on master branch, only on the version 1.0.0-dev.11

  • for some reasons, changing the onPressed of your example to:

    onPressed: () {
      ref.read(fooStateNotifierProvider.notifier).sayHello();
      ref.read(fooStateNotifierProvider); // <- added
    },

    fixes the issue

@rrousselGit rrousselGit removed the question Further information is requested label Oct 26, 2021
@KoheiKanagu
Copy link
Contributor

Thanks for checking.
I was able to solve the problem in my project.

@tomoyuki28jp
Copy link

My app uses the latest version of riverpod on master branch, but it sometimes throws this error :(

@rrousselGit
Copy link
Owner

@tomoyuki28jp could you share a way to reproduce the problem in your case?

I don't have the issue on master.

@ethras
Copy link

ethras commented Nov 2, 2021

Facing the same issue randomly on 1.0.0-dev.11. But seems to be fixed on master branch

edit: Just happened again on master branch :/ was working fine on dev 7. No await in the provider code

@mono0926
Copy link
Sponsor Contributor Author

mono0926 commented Nov 8, 2021

With #815 (comment) code and this, the error occurs on 1.0.0 stable release.

void main() {
  runApp(
    const ProviderScope(
      child: MaterialApp(
        home: FirstScreen(),
      ),
    ),
  );
}
  1. Tap Push First button
  2. Tap Push Second button
  3. The error occurs
  4. The navigation is done successfully

The error message is Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt, but the log isn't printed at Console 🤔

image

@tbm98
Copy link
Contributor

tbm98 commented Nov 8, 2021

my issue #831 no longer exists. maybe your issue and mine are not the same

@chunghha
Copy link
Contributor

chunghha commented Nov 21, 2021

I see same exception. For my use case, the exception happens only once while redirecting to a path with go_router while app alive. Here is to reproduce the exception below. chunghha/fl_pages@7c08ec5

I may be probably missing to check something so keep digging in but also I'd like to share for a help.

Performing hot restart...                                               
Restarted application in 571ms.
flutter: GoRouter: known full paths for routes:
flutter: GoRouter:   => /
flutter: GoRouter:   => /polygon
flutter: GoRouter:   => /login
flutter: GoRouter: setting initial location /
flutter: |.. _routeToPath: /
flutter: \.... _authStatus: AuthStatus.unauthenticated
flutter: GoRouter: redirecting to /login
flutter: |.. _routeToPath: /login
flutter: \.... _authStatus: AuthStatus.unauthenticated
flutter: |.. _routeToPath: /login
flutter: \.... _authStatus: AuthStatus.unauthenticated
flutter: token 0002BIhGyCWXtb0YpIeRvED8TZ6R
flutter: \^[[38;5;12m┌───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<>
flutter: \^[[38;5;12m│ #0   printAppVersion (package:fl_pages/util/app_version.dart:12:13)<…>
flutter: \^[[38;5;12m│ #1   <asynchronous suspension><…>
flutter: \^[[38;5;12m├┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄┄<>
flutter: \^[[38;5;12m│ 💡 App Version: 1.1.0+1.1.0<>
flutter: \^[[38;5;12m└───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────<>
flutter: GoRouter: going to /
flutter: ══╡ EXCEPTION CAUGHT BY FLUTTER FRAMEWORK ╞═════════════════════════════════════════════════════════
flutter: The following _Exception was thrown Exception during GoRouter navigation:
flutter: Exception: 'package:riverpod/src/framework/provider_base.dart': Failed assertion: line 645 pos 7:
flutter: '_debugDidChangeDependency == false': Cannot use ref functions after the dependency of a provider
flutter: changed but before the provider rebuilt
flutter:
flutter: When the exception was thrown, this was the stack:
flutter: #2      ProviderElementBase._assertNotOutdated (package:riverpod/src/framework/provider_base.dart:645:7)
flutter: #3      ProviderElementBase.watch (package:riverpod/src/framework/provider_base.dart:691:5)
flutter: #4      appRouter.<anonymous closure>.<anonymous closure> (package:fl_pages/app.router.dart:49:15)
flutter: #5      GoRouterDelegate._getLocRouteMatchesWithRedirects (package:go_router/src/go_router_delegate.dart:323:22)
flutter: #6      GoRouterDelegate._go (package:go_router/src/go_router_delegate.dart:249:21)
flutter: #7      GoRouterDelegate.go (package:go_router/src/go_router_delegate.dart:149:5)
flutter: #8      GoRouter.go (package:go_router/src/go_router.dart:78:22)
flutter: #9      GoRouterHelper.go (package:go_router/go_router.dart:32:25)
flutter: #10     goToPage (package:fl_pages/app.router.dart:87:11)
flutter: #11     LoginButton.build.<anonymous closure> (package:fl_pages/widgets/login_button.widget.dart:39:11)
flutter: <asynchronous suspension>
flutter: (elided 2 frames from class _AssertionError)
flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════
flutter: GoRouter: known full paths for routes:
flutter: GoRouter:   => /
flutter: GoRouter:   => /polygon
flutter: GoRouter:   => /login
flutter: GoRouter: setting initial location /
flutter: |.. _routeToPath: /
flutter: \.... _authStatus: AuthStatus.authenticated
flutter: GoRouter: refreshing /
flutter: |.. _routeToPath: /
flutter: \.... _authStatus: AuthStatus.authenticated
flutter: |.. _index onTap: 1
flutter: GoRouter: going to /polygon
flutter: |.. _routeToPath: /polygon
flutter: \.... _authStatus: AuthStatus.authenticated
flutter: |.. _index onTap: 0
flutter: GoRouter: going to /
flutter: |.. _routeToPath: /
flutter: \.... _authStatus: AuthStatus.authenticated
Application finished.

@chunghha
Copy link
Contributor

I have my fix

@rrousselGit
Copy link
Owner

For reference, this should be fixed in the 2.0.0 dev release.

@pcelis19
Copy link

For reference, this should be fixed in the 2.0.0 dev release.

Do you know where I can find the notes for 2.0.0-dev.0 because I am still getting the same issue:

'package:riverpod/src/framework/provider_base.dart': 
Failed assertion: line 657 pos 7: '_debugDidChangeDependency == false': 
Cannot use ref functions after the dependency of a provider changed but before the provider rebuilt

@rrousselGit
Copy link
Owner

@pcelis19 Please provide a way to reproduce your issue. As mentioned before it could very well be that it's your code the problem

@Scheideba
Copy link

For reference, this should be fixed in the 2.0.0 dev release.

This worked for me thank you

@sumanchapai
Copy link

For me, I noticed that i was using watch where I should have used read.

    ref
        .read(todayRoutineStatusProvider.state)
        .update((_) => TodayRoutineStatus.loading);

Was to be used instead of

    ref
        .watch(todayRoutineStatusProvider.state)
        .update((_) => TodayRoutineStatus.loading);

@Yogesh-Dubey-Ayesavi
Copy link

For information: This happens typically happens if you're using ref.something on a provider that was disposed.

It could be that you're using a FutureProvider like so:

final provider = FutureProvider((ref) {
  ref.watch(foo);
  await something();
  ref.watch(bar);
})

But during the await something, the provider was disposed (because foo changed), causing ref.watch(bar) to fail with this error

What should i do in this case?

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

No branches or pull requests