Navigator operation requested with a context that does not include a navigator
Navigator operation requested with a context that does not include a navigator
Flutter Navigator not working
I have app with two screens, and I want to make push from 1st to second screen by pressing button.
Screen 1
Screen 2
Push not happening and I got this
The following assertion was thrown while handling a gesture: Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
7 Answers 7
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
Think of the widgets in Flutter as a tree, with the context pointing to whichever node is being built with the build function. In your case, you have
So when you’re using the context to find the Navigator, you’re using a context for the MainScreen which isn’t under the navigator.
You can either make a new Stateless or Stateful Widget subclass to contain your Center + FlatButton, as the build function within those will point at that level instead, or you can use a Builder and define the builder callback (which has a context pointing at the Builder) to return the Center + FlatButton.
Just make the MaterialApp class in main method as this example
it works fine for me, I hope it will work with you
There are two main reasons why the route cannot be found.
2) The Route is defined on the sibling branch e.g.
If we want to dispatch a route from the Navigation widget, we have to know the reference to the NavigatorState. Having a global reference is expensive, especially when you intend to move widget around the tree. https://docs.flutter.io/flutter/widgets/GlobalKey-class.html. Use it only where there is no way to get it from Navigator.of(context).
To use a GlobalKey inside the MaterialApp define navigatorKey
Now anywhere in the app where you pass the navigatorKey you can now invoke
how to fix flutter exception : Navigator operation requested with a context that does not include a Navigator
I am trying to create a drawer navigation using flutter framework, but i am getting the following exception every time I run it
Another exception was thrown: Navigator operation requested with a context that does not include a Navigator.
I used Navigator class as the following
and the code of the NextPage class is
3 Answers 3
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
It looks like you don’t have a Navigator setup for current context. Instead of using StatefulWidget you should try MaterialApp as your root App. MaterialApp manages a Navigator for you. Here is an example of how to setup an App in your main.dart
This is because the context that you’re using is from the app level before a Navigator has actually been created. This is a common problem when creating «simple» single file apps in Flutter.
There are a number of possible solutions. One is to extract your Drawer into it’s own class (extend Stateless/StatefulWidget accordingly), then in it’s build override, the parent Scaffold will have already been created containing a Navigator for you to use.
The other, if you want to keep this Drawer in the same file, is to use a Builder instead, which has the same effect:
you need to create a new Widget as home in MaterialApp like this:-
(This worked for me)
Not the answer you’re looking for? Browse other questions tagged android flutter or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
How to fix «Navigator operation requested with a context that does not include a Navigator» when attempting to display a dialog
I am trying to show a non dismissable dialog after verifying the textfields in a form but it keeps printing:
but refused to show the dialog.
Please what am I doing wrong?
Part of my Code
4 Answers 4
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
The issue could be because of the MaterialApp context in the library. Creating a new Widget as home in MaterialApp may solve this
I struggled with this problem some days ago.
After a lot of exception trapping and printing, I solved a similar problem by wrapping all of the body property of the Scaffold in a Builder widget and using Builder ‘s context as the context to use for Navigator operations.
More about this, with an explaination, can be found under the BuildContext documentation page:
Following a similar pattern helped me solve my problem.
Edit: here’s a code sample, where my build method returns a Builder that wraps actual wiget building.
Flutter Error: Navigator operation requested with a context that does not include a Navigator #15919
Comments
1SouravGhosh commented Mar 25, 2018 •
Steps to Reproduce
Exception: «Another exception was thrown: Navigator operation requested with a context that does not include a Navigator. »
lib.zip
Run your application with flutter run and attach all the log output.
Run flutter analyze and attach any output of that command also.
Flutter Doctor
For more information about diagnosing and reporting Flutter bugs, please see https://flutter.io/bug-reports/.
The text was updated successfully, but these errors were encountered:
im-minion commented Apr 7, 2018
suffering the same issue 🙁
burhanrashid52 commented Apr 24, 2018 •
There is some issue with MateriaApp context in the library.The Navigator won’t work if you’re doing in under MateriaApp context like this
(This wont work)
Instead, you need to create a new Widget as home in MaterialApp like this
(This will work)
m3ck0 commented May 2, 2018
Thank you very much @burhanrashid52, I had same issue and your solution works like a charm. In addition I think that the correct solution is more expressive rather than the first one.
gagangoku commented May 29, 2018
Thank you very much @burhanrashid52, struggled with this error. Its not obvious and definitely a limitation i feel.
SG14-96 commented Jun 5, 2018
I have a similar issue.
actions: [ new IconButton(icon: new Icon(Icons.account_circle), onPressed: () < Navigator.push(context, new MaterialPageRoute(builder: (context) =>new AccountScreen())); >), ],
awg01 commented Jul 11, 2018 •
I have similar issue but I solved it by using your trick..
Here is my correct code.
1SouravGhosh commented Jul 15, 2018
This is issue has been resolved. Please refer to the comments above.
burhanrashid52 commented Jul 16, 2018
On which version its resolved? @1SouravGhosh
zoechi commented Jul 16, 2018
@burhanrashid52 it was not a bug in Flutter, but in the custom code.
1SouravGhosh commented Jul 16, 2018
Yes in my case there was the problem in the code itself. I followed the above comments and fixed it, thus I closed the issue.
Aanu1995 commented Dec 15, 2018
I was faced with this problem but now it has been solved. Thanks to you
haiderkhalil commented Jan 22, 2019
mdabdullahbaig commented Feb 21, 2019
Thanks for your suggestion. to create a HomePage();
danyismail commented May 11, 2019
thanks u so much.
ukadawit commented May 18, 2019
Thanks. It solved my problem.
phanirithvij commented May 26, 2019 •
How to do this for statefulwidgets no examples anywhere.
@burhanrashid52
ilyamolokov commented May 27, 2019
Thank you, your solution works with StatelessWidget.
But how to do this for StatefulWidget?
@burhanrashid52
jithin-pal commented Jul 18, 2019
i have multiple pages and when i use your fix, i still keep getting the same error.
dmitrijkiltau commented Aug 28, 2019
For those who ask: The @burhanrashid52 solution works the same for StatefulWidget.
CodePhilanthropist commented Oct 12, 2019 •
Can you please provide example for StatefulWidgets, please.
dae54 commented Nov 10, 2019
was helped by it too. great charm
ritheesh4 commented Dec 3, 2019
There is some issue with MateriaApp context in the library.The Navigator won’t work if you’re doing in under MateriaApp context like this
(This wont work)
Instead, you need to create a new Widget as home in MaterialApp like this
(This will work)
thanks man. you fixed my error.
bekagaw653 commented Jan 11, 2020
mohammedmoheed commented Mar 5, 2020
I have similar issue but I solved it by using your trick..
Here is my correct code.
yvonmanzi commented Apr 20, 2020
Thanks guys! had the same issue for a few hours, but this thread helped me fix it!
vinothvino42 commented Apr 24, 2020 •
Exception:
The following assertion was thrown while handling a gesture:
Navigator operation requested with a context that does not include a Navigator.
The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
Not Working
But the same widget without a MediaQuery works fine.
Working
tsniveda commented Jun 9, 2020
There is some issue with MateriaApp context in the library.The Navigator won’t work if you’re doing in under MateriaApp context like this
(This wont work)
Instead, you need to create a new Widget as home in MaterialApp like this
(This will work)
Thank you! Works seamlessly!
albykennady commented Jun 10, 2020
There is some issue with MateriaApp context in the library.The Navigator won’t work if you’re doing in under MateriaApp context like this
(This wont work)
Instead, you need to create a new Widget as home in MaterialApp like this
(This will work)
hey you helped me fix the error but can you explain why are we doing this?
lock bot commented Jun 24, 2020
Footer
© 2022 GitHub, Inc.
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Navigator operation requested with a context that does not include a Navigator
I’m trying to start a new screen within an onTap but I get the following error:
Navigator operation requested with a context that does not include a Navigator.
The code I am using to navigate is:
I have set up a route in my app as follows:
I’ve tried to copy the code using the stocks sample application. I’ve looked at the Navigator and Route documentation and can’t figure out how the context can be made to include a Navigator. The context being used in the onTap is referenced from the parameter passed into the build method:
SettingsPage is a class as follows:
16 Answers 16
Trending sort
Trending sort is based off of the default sorting method — by highest score — but it boosts votes that have happened recently, helping to surface more up-to-date answers.
It falls back to sorting by highest score if no posts are trending.
Switch to Trending sort
This error is unrelated to the destination. It happens because you used a context that doesn’t contain a Navigator instance as parent.
In the first case, everything is fine. In the second, it throws a
Navigator operation requested with a context that does not include a Navigator.
First, let’s reproduce this error :
This example creates a button that attempts to go to ‘/’ on click but will instead throw an exception.
Notice here that in the
There are a few ways to achieve this. You can extract home into a custom class :
Источники информации:
- http://stackoverflow.com/questions/51600412/how-to-fix-flutter-exception-navigator-operation-requested-with-a-context-that
- http://stackoverflow.com/questions/49427825/how-to-fix-navigator-operation-requested-with-a-context-that-does-not-include-a
- http://github.com/flutter/flutter/issues/15919
- http://stackoverflow.com/questions/44004451/navigator-operation-requested-with-a-context-that-does-not-include-a-navigator