Workaround to "cannot use "ref" after the widget was disposed" #3043
-
Hello everyone,
This is a minimal example displaying the issue:
If I understand the problem correctly the The solution I've found is to either turn What I'm asking is what is the best practice in cases like this. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
You can just update your onPressed to: onPressed: () async {
ref.read(loginStateProvider.notifier).loggingIn();
await Future.delayed(const Duration(seconds: 1)); // Loggin in
if (!context.mounted) return; //Return if widget is disposed.
ref.read(loginStateProvider.notifier).loggedIn();
}, |
Beta Was this translation helpful? Give feedback.
-
If anyone hits this page on a search of the above error, I think that this is a general answer for when the scope of a ref is coming to an end. For example if you click on a button on a context menu or some other menu. When you close the menu then the ref may go out of scope and you cannot use it after this time. But you may be making async calls which take time. So follow this logic:
|
Beta Was this translation helpful? Give feedback.
@Maldus512 You can just do: