Skip to content
tezvyn:

How do you pass data with Navigator.push and return data with Navigator.pop?

Source: docs.flutter.devEasyHow cards are made

Navigator.push<T> returns a Future for the value supplied to Navigator.pop. Pass data into the destination constructor, await a nullable result, and check mounted before using the parent context after the await.

Navigator.push<T> returns a Future<T?> that completes when the route is popped. Pass input through the destination constructor, return the selected value from the child, and handle a null result when the route is dismissed.

Example

This fragment goes inside a State method. locale is existing state, and LanguagePicker accepts initialLocale.

final Locale? picked = await Navigator.push<Locale>(
  context,
  MaterialPageRoute<Locale>(
    builder: (_) => LanguagePicker(initialLocale: locale),
  ),
);
if (!context.mounted || picked == null) return;
setState(() => locale = picked);

// In LanguagePicker, after the user chooses a locale:
Navigator.pop(context, const Locale('es'));

Watch out for

The Future-based handshake suits a small parent-child interaction. A declarative router or shared state may fit a larger flow. After any await, check that the context is still mounted before using it.

Interview question

How should a parent screen correctly capture data sent back from a route it pushed with Navigator.push?

  • a.Use the child route's BuildContext to call setState on the parent after Navigator.pop.
  • b.Store the result in a global state object before calling Navigator.pop in the child.
  • c.Await the Future returned by Navigator.push, which completes with the Navigator.pop result.Correct
  • d.Assign Navigator.push to a variable and read the result synchronously on the next line.
Why?

Navigator.push returns a Future that completes with the value passed to Navigator.pop, so awaiting it is the correct pattern. Reading it synchronously fails because the route is still active and the result is not yet available.

Keep this idea fresh

Try a few questions or explore another bite in Flutter & Dart.

Read the original → docs.flutter.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles