Closed ffpetrovic closed 3 years ago
Also, maybe a useful piece of information -- when this assignment is executed:
useEffect(() {...}, [])
hook is called again?if(navigator.state == null)
Actually what I just ended up doing to achieve my goal was
final navigatorStateProvider = StateProvider<GlobalKey<NavigatorState>>(
(ref) => GlobalKey<NavigatorState>(),
);
But I'd still love an answer as to why the above is throwing an error, and maybe the rerunning of the useEffect hook peculiarity too?
You can't create GlobalKeys like that. You need to keep a reference on them, or otherwise if your widget is somehow recreated, the GlobalKey will be recreated too.
Considering you fixed your problem by caching the GlobalKey, that is likely what your problem was.
#0 StateNotifier.state= package:state_notifier/state_notifier.dart:173:7)
so it doesn't really look like the problem is regarding the GlobalKey, but the state provider?Thanks for your answer anyway! 👍
Ah I misread your code
Your issue is instead that you modified a provider inside build
, which is invalid.
You mean the navigator.state = navigatorKey;
line?
But I modified it inside a side-effect, why is that invalid? The useEffect hook is supposed to be inside the build method, right?
Describe the bug Setting StateProvider state value throws error.
To Reproduce
Expected behavior I expect the state of this provider to be set without an error. The stack trace is
Also, as you can see, what I'm trying to do, is make a provider with the state of
GlobalKey<NavigatorState>
so I can navigate inside other providers. Is there something else that I'm doing wrong, or is this the correct approach?