Get a Result Back
Part of the Lists and Screens section of Coddy's Flutter journey. Lesson 22 of 33.
Some screens exist to ask a question: pick a color, pick a date. The answer has to travel back to the screen that asked. Navigator.pop can carry it:
Navigator.pop(context, 'Green');On the other side, Navigator.push gives back a Future: a value that arrives later, when the pushed screen is popped. await waits for it, and the function that waits is marked async:
onPressed: () async {
final String? color = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ColorScreen()),
);
if (color != null) {
setState(() {
chosen = color;
});
}
},The result is a String?: when someone leaves with the back arrow, nothing is sent and color is null. The if keeps the old choice then. A later section tells the whole story of Future and await.
Challenge
MediumPicking a color closes the color screen, but the home screen never hears about it. Make the choice come back:
- on the color screen, pop with the color:
Navigator.pop(context, color) - on the home screen,
awaitthe push and, when a color came back, showYour color: Green(with the color picked) - leaving with the back arrow keeps the old text
Try it yourself
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return const MaterialApp(
debugShowCheckedModeBanner: false,
home: HomeScreen(),
);
}
}
class HomeScreen extends StatefulWidget {
const HomeScreen({super.key});
@override
State<HomeScreen> createState() => _HomeScreenState();
}
class _HomeScreenState extends State<HomeScreen> {
String chosen = 'none';
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Paint')),
body: Center(
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
Text('Your color: $chosen', style: const TextStyle(fontSize: 24)),
const SizedBox(height: 16),
ElevatedButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(builder: (context) => const ColorScreen()),
);
},
child: const Text('Choose a color'),
),
],
),
),
);
}
}
class ColorScreen extends StatelessWidget {
const ColorScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick a color')),
body: ListView(
children: [
for (String color in ['Red', 'Green', 'Blue'])
ListTile(
title: Text(color),
onTap: () {
Navigator.pop(context);
},
),
],
),
);
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Lists and Screens
1Scrolling Lists
A List That ScrollsList TilesLeading and TrailingTap a List TileDividers Between Rows5More Than One Screen
Open a New ScreenThe Back ArrowGo Back With a ButtonSend Data to a ScreenGet a Result Back6Tabs, Drawers and Swipes
Tabs With a Navigation BarFilled Icons for the Open TabA Side DrawerSwipe to DeleteUndo a SwipePractice on your own: Online Flutter compiler