Sub topic 4
Getting a Result Back from a Screen
This chapter assumes you know Dart async and await, basic Flutter widgets, and how BuildContext identifies a widget's location in the widget tree.
A Flutter Navigator manages a stack of routes. A route is usually a screen. When you call push, Flutter adds a route to the top of the stack. The push method returns a Future, which completes when that route is removed. The destination screen can provide a result by passing a value to pop.
Returning a value with pop
The generic type on Navigator.push<T> describes the result type. In this example, the result is a String.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: HomeScreen()));
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
Future<void> pickColor(BuildContext context) async {
final color = await Navigator.of(context).push<String>(
MaterialPageRoute<String>(
builder: (context) => const ColorPickerScreen(),
),
);
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Picked: ${color ?? 'nothing'}')),
);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Home')),
body: Center(
child: ElevatedButton(
onPressed: () => pickColor(context),
child: const Text('Choose a color'),
),
),
);
}
}
class ColorPickerScreen extends StatelessWidget {
const ColorPickerScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Pick a color')),
body: Center(
child: ElevatedButton(
onPressed: () => Navigator.of(context).pop('red'),
child: const Text('Red'),
),
),
);
}
}Navigator.of(context).pop('red') removes the color picker and completes the waiting Future with 'red'. The color variable in pickColor then contains that value.
The result must match the type declared on push. For example, push<int> should return an integer, while push<bool> should return a Boolean. Use a small model class when the destination needs to return several related values.
Handle cancellation and lifecycle changes
A user can leave the destination with the system back button or the app bar's back button. Those actions usually complete the Future with null, so the caller should handle a nullable result.
The caller also needs to check context.mounted after await. While the destination was open, Flutter might have removed the calling widget. Using its context after removal can cause an error. The check prevents code from showing a snackbar or updating state from a no-longer-mounted widget.
> callout note > Treat a returned result as optional unless every possible way to leave the screen supplies a value.
Choosing a result type
| Need | Suitable result |
|---|---|
| User selects one item | `String?` or an item model |
| User confirms or cancels | `bool?` |
| Form returns several fields | A dedicated data class |
| Screen only displays information | `void` |
Exercise
What value does the caller receive if the user presses the system back button instead of the Red button?
Show answer
The pushed Future normally completes with null, so the caller must handle the nullable result.