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.
Related
Is someone able to explain to me why 'context' in there is undefined? i Watched like 5 videos about BuildContext and i still don't understand it. Yes i'm beginner with dart
class ZmienneClass extends ChangeNotifier {
void decrementCounter(int liczba) {
if (_rundy == 0) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => resGamePage(title: "")));
};}}
The BuildContext is needed to tell Flutter where it should build its widgets. Currently you are trying to access a context without actually providing a valid BuildContext, if you really want to access the context, you could provide it trough the decrementCounter by passing one more parameter in it, decrementCounter(int liczba, BuildContext context) and pass the context from where you are calling it.
You dont declare BuildContext try below code refer BuildContext here
class ZmienneClass extends ChangeNotifier {
void decrementCounter(int liczba,BuildContext context) {
if (_rundy == 0) {
Navigator.push(context,
MaterialPageRoute(builder: (context) => resGamePage(title: "")));
};}}
I'm trying to add flutter_bloc to my flutter application. I have Home page and when click on a button in home page Need to Navigation to a different page. Navigation is working without any issue, But When Wrap the the component with a BlocProvider getting below wrror
The argument type 'MyBloc' can't be assigned to the parameter type 'MyBloc Function(BuildContext)'
This what I have tried so far
My Bloc
import 'package:flutter_bloc/flutter_bloc.dart';
enum MyEvent {increment, decrement}
class MyBloc extends Bloc<MyEvent, int> {
MyBloc() : super(0);
#override
Stream<int> mapEventToState(MyEvent event) async* {
switch (event) {
case MyEvent.increment:
yield state + 1;
break;
case MyEvent.decrement:
yield state - 1;
break;
}
}
}
In home page I'm calling below function when click on button
Future<void> _routeToTodayTasks() {
return Navigator.push(
context,
MaterialPageRoute(
builder: (BuildContext context) => BlocProvider<MyBloc>(
create: MyBloc(),
child: TodayTasks(),
)));
}
Could anyone tell me what is wrong with my code?
As the error message clearly states, the create argument in BlocProvider is accepting a function with syntax MyBloc Function(BuildContext), but instead you are returning an object of your MyBloc class directly.
So, returns the bloc object from a function like this:
create: (context) => MyBloc(),
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.
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();
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.