What does MaterialPageRoute with the context?

Viewed 477

What does MaterialPageRoute do with context? And what is the purpose of builder: here.

MaterialPageRoute(builder: (context) {
                  return Scaffold(
                    appBar: AppBar(
                      title: Text("Second Route"),
                    ),
                    body: Center(
                      child: RaisedButton(
                        onPressed: () {
                          Navigator.pop(context);
                        },
                        child: Text('Go back!'),
                      ),
                    ),
                  );
                }
1 Answers

Every state of widget required some context to perform some work, direct or indirect context should be used.

The [BuildContext] for a particular widget can change location over time as the widget is moved around the tree. Because of this, values returned from the methods in this class should not be cached beyond the execution of a single synchronous function.

Example:

In the question code for back navigation used Navigator.pop(context); so it will be running in a separate state, not the build(contex) from where navigation starts

Related