Sub topic 3
Passing Data to the Next Screen
This chapter assumes Dart async and await, basic Flutter widgets, and familiarity with BuildContext, Scaffold, and Navigator. It focuses on sending data when a screen opens. Returning data to the previous screen is covered separately.
Use constructor parameters for direct navigation
A screen is a widget, so the simplest way to pass data is through its constructor. This makes the required data visible in the widget's API and gives you static type checking.
In this example, ProductScreen requires an integer product ID:
import 'package:flutter/material.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
theme: ThemeData(useMaterial3: true),
home: const HomeScreen(),
);
}
}
class HomeScreen extends StatelessWidget {
const HomeScreen({super.key});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Products')),
body: Center(
child: ElevatedButton(
onPressed: () {
Navigator.of(context).push(
MaterialPageRoute<void>(
builder: (context) => const ProductScreen(productId: 42),
),
);
},
child: const Text('Open product 42'),
),
),
);
}
}
class ProductScreen extends StatelessWidget {
final int productId;
const ProductScreen({super.key, required this.productId});
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Product')),
body: Center(
child: Text('Loading product $productId'),
),
);
}
}MaterialPageRoute receives a builder. The builder creates ProductScreen with the value to pass. The destination stores that value in a final field.
Use this approach when the caller already knows the destination widget and its data. It is explicit, easy to refactor, and checked by the Dart analyzer.
Pass arguments through named routes
Named routes separate navigation commands from widget construction. The caller supplies a route name and an arguments value:
Navigator.of(context).pushNamed(
'/product',
arguments: 42,
);The route configuration reads the value and passes it to the destination:
MaterialApp(
initialRoute: '/',
onGenerateRoute: (settings) {
switch (settings.name) {
case '/':
return MaterialPageRoute<void>(
builder: (_) => const HomeScreen(),
settings: settings,
);
case '/product':
final productId = settings.arguments as int;
return MaterialPageRoute<void>(
builder: (_) => ProductScreen(productId: productId),
settings: settings,
);
default:
return MaterialPageRoute<void>(
builder: (_) => const NotFoundScreen(),
settings: settings,
);
}
},
);The as int cast fails at runtime if a caller supplies another type. For values from outside your control, validate the argument instead of assuming its type.
Choose a data-passing method
| Method | Strength | Common risk |
|---|---|---|
| Constructor parameter | Explicit and type-safe | The caller depends on the widget class |
| Named route argument | Central route configuration | Runtime casts can fail |
| Path parameter in a router | Works well with URLs and deep links | Values arrive as strings and need parsing |
The data should usually be an identifier, such as productId, rather than a large mutable model. The destination can then load the current product from its state or repository.
Exercise
Which approach is usually the best choice when a screen is opened directly with a known, required value?
Show answer
Pass the value through a required constructor parameter, because the dependency is explicit and checked by the Dart analyzer.