Undefined name 'context' in flutter navigation - flutter

I want to navigate into one page to another an I used following code to that
Create route
final routes = <String,WidgetBuilder> {
DashboardIesl.tag : (context)=>DashboardIesl()
};
navigation to button click
onPressed: () {
Navigator.push(context, new MaterialPageRoute(
builder: (context) => new DashboardIesl()
));
},
It gives the error message as follows
Undefined name 'context'.

please show me more code,context is only available in statefull widgets,
onPressed: () { Navigator.push(
context,
MaterialPageRoute(builder: (context) => DashboardIesl()), );}
I think this will help you

You have to write onPressed() with in the class.

Related

element data not refreshing

//calling a different class for dialog on submit click it's not updating list data
It's refreshing data but not going to the last opened tab on the second tab I perform this action but on tap, it's going on the first tab.
onTap: () {
setState(() {
Navigator.pushReplacement(context,
MaterialPageRoute(builder: (context) =>
EmployeePersonalDetailsUpdateActivity(_token),));
widget.listener.onEmployeeFamilyClick(_day, _month, _year,
_working);
});
Navigator.pop(context,true);
}
Your are calling your widget.listener after you navigate, call it before you navigate the route & if you want to pass the api response to the EmployeePersonalDetailsUpdateActivity widget, you must handle it, and it's your choice when you want to refresh your screen.
setState(() {
widget.listener.onEmployeeFamilyClick(_day,_month,_year,_working);
Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => EmployeePersonalDetailsUpdateActivity(_token),));
});
There's another way:
widget.listener.onEmployeeFamilyClick(_day,_month,_year,_working).then((value) {
Navigator.pushReplacement(context, MaterialPageRoute(builder:
(context) => EmployeePersonalDetailsUpdateActivity(_token), value));
});
you can navigate your widget and pass the value of the response and show the updated value on the widget.

Named MaterialPageRoute?

I currently have
Navigator.pushReplacementNamed(context, "/second", arguments: contact);
...
How would one rewrite this as a MaterialPageRoute?
Contact contact = ModalRoute.of(context).settings.arguments;
.
'/second': (BuildContext context) => ViewContact(),
Yes We can do this by passing the name in the route setting parameter of material page route. Please refer to the following piece of code:
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => VendorDetailScreen(widget.vendor),//pass any arguments
settings: RouteSettings(name: "vendorScreen")),//assign a name for the screen
In this way we can assign the name to the material page route. Now your navigation stack will have a route with the name "vendorScreen" after you push the screen.
You can use a MaterialPageRoute passing contact as argument in this way:
Navigator.pushReplacement(
context, MaterialPageRoute(builder: (BuildContext context) => ViewContact(contact)));
}
And then in the ViewContact page:
class ViewContact extends StatelessWidget {
Contact contact;
ViewContact(this.contact);
...
}
I think that you were asking for something like that:
Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => ViewContact(contact),
),
);
Just use pushNamed/pushReplacementNamed.

Flutter: Navigator.pop(context);

I pass a page with this code below.
Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
for example, I change some data in FileFolder(); page. And I want to get this data to my first page what I change data. I use Navigator.pop(context); but this code is not run initstate((){});. How can I refresh my first page?
//first page
Future data = await Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
setState(() {
myData = data;
});
//second page FileFolder
Map data = {};
Navigator.pop(context,data);
You can write your code inside initstate((){}); method like below:
Future.delayed(Duration.zero, () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => FileFolder()));
});

How does this Flutter constructor call work?

I am new to Flutter and as I was reading through the tutorial I saw the following code snippet:
// Within the `FirstRoute` widget
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => SecondRoute()),
);
}
Why does the call in the MaterialPageRoute work? I see it requests an object of type WidgetBuilder, but what we pass is a BuildContext. Do the parentheses around context indicate a call to the constructor of the WidgetBuilder?
The builder parameter accepts an object of type WidgetBuilder as you say. What is "hidden" is that it is typedef:
typedef WidgetBuilder = Widget Function(BuildContext context);
So what you've passed is actually an anonymous function that match it.
It is called an arrow function. Actually this piece of code
(context) => SecondRoute()
can be rewritten like:
MaterialPageRoute(builder: (context) {
return SecondRoute();
})
And widget builder is a function, which MaterialPageRoute require as a parameter.

Flutter/Dart - Pass Context Forward in order to Navigator.pop two Contexts back?

I need to be able to pop back two pages. As I'm creating pages on the fly using a named route wouldn't be practical. So is there a way to pass context from the original page to the destination page?
On the original page I have;
Navigator.push(
context,
MaterialPageRoute(builder: (originalcontext) => SecondRoute()),
);
Then on that SecondRoute's page I have:
Navigator.push(
context,
MaterialPageRoute(builder: (originalcontext) => ThirdRoute()),
);
Then on the ThirdRoute page, I'd like to pop back to the original page - bypassing the SecondRoute page.
Navigator.pop(originalcontext);
You can define your SecondRoute like this :
class SecondRoute extends StatelessWidget
{
final BuildContext originalContext;
//other properties
SecondRoute({this.originalContext}); //Initialize other properties
//Now use the 'originalContext'
}
Similarly you can define your ThirdRoute .
Now pass the originalContext ,
Navigator.push(
context,
MaterialPageRoute(builder: (originalcontext) => SecondRoute(originalContext:originalcontext)),
);
Edit : The original question has been edited.
First Method:
Try this :
Navigator.of(context).popUntil((route) => route.isFirst)
put the above code on your
ThirdRoute()
Second Method :
put this on your SecondRoute()
Navigator.pushReplacement(context, MaterialPageRoute(builder: (BuildContext context) => ThirdRoute()));
instead of plain Navigator.push(...);
And on your ThirdRoute() add this
Navigator.of(context).pop();