Sometimes you want to send information from one screen to another in your app. Passing data between screens helps you share details like user choices or messages.
Passing data between screens in Flutter
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondScreen(data: yourData),
),
);Use MaterialPageRoute to create a route to the new screen.
Pass data as parameters to the new screen's constructor.
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DetailScreen(item: selectedItem),
),
);Navigator.push(
context,
MaterialPageRoute(
builder: (context) => MessageScreen(message: 'Hello!'),
),
);This app has two screens. The first screen has a button. When you tap it, it goes to the second screen and sends a message. The second screen shows that message in big text.
import 'package:flutter/material.dart'; void main() => runApp(MyApp()); class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { return MaterialApp( home: FirstScreen(), ); } } class FirstScreen extends StatelessWidget { @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('First Screen')), body: Center( child: ElevatedButton( child: Text('Go to Second Screen'), onPressed: () { Navigator.push( context, MaterialPageRoute( builder: (context) => SecondScreen(data: 'Hello from First Screen!'), ), ); }, ), ), ); } } class SecondScreen extends StatelessWidget { final String data; SecondScreen({required this.data}); @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar(title: Text('Second Screen')), body: Center( child: Text(data, style: TextStyle(fontSize: 24)), ), ); } }
Always pass data through the constructor of the new screen for clarity.
You can pass any type of data: strings, numbers, objects, or lists.
Remember to mark constructor parameters as required if they must be provided.
Passing data between screens lets you share information as you move through your app.
Use Navigator.push with a route that builds the new screen and passes data via constructor.
This keeps your app interactive and connected across screens.
Practice
Solution
Step 1: Understand screen navigation
When users move between screens, apps often need to share information to keep context.Step 2: Identify the role of data passing
Passing data allows the next screen to display or use information from the previous screen.Final Answer:
To share information as the user moves from one screen to another -> Option DQuick Check:
Passing data = share info between screens [OK]
- Confusing data passing with theme or style changes
- Thinking data passing stores data permanently
- Assuming data passing speeds up app startup
Solution
Step 1: Check constructor usage
Passing data requires calling the screen's constructor with named or positional parameters.Step 2: Verify syntax correctness
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello"))); correctly calls DetailScreen with named parameter data: "Hello" inside the builder.Final Answer:
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello"))); -> Option CQuick Check:
Correct constructor call = Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello"))); [OK]
- Omitting parentheses after DetailScreen
- Passing data without parameter name when required
- Using incorrect method calls on DetailScreen
class DetailScreen extends StatelessWidget {
final String message;
DetailScreen({required this.message});
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(child: Text(message)),
);
}
}
// Navigation call:
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(message: "Welcome")));
Solution
Step 1: Analyze constructor parameter
DetailScreen requires a String message passed via constructor, which is done correctly with "Welcome".Step 2: Check build method output
The build method shows a Text widget displaying the message variable, so it will show "Welcome".Final Answer:
Text widget showing "Welcome" -> Option BQuick Check:
Passed message = displayed text [OK]
- Assuming message is not passed causes error
- Thinking Text shows variable name instead of value
- Ignoring the required keyword in constructor
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen));
Solution
Step 1: Check DetailScreen usage
DetailScreen is a class and must be instantiated by calling its constructor with parentheses.Step 2: Identify missing constructor call
Without parentheses, DetailScreen is a type, not an instance, causing a syntax error.Final Answer:
DetailScreen is missing parentheses to call its constructor -> Option AQuick Check:
Constructor call needs parentheses [OK]
- Forgetting parentheses after widget class name
- Misunderstanding Navigator.push usage
- Thinking context is optional in navigation
Solution
Step 1: Understand data passing direction
Navigator.push sends data forward to a new screen via constructor parameters.Step 2: Identify correct data usage
Passing userId via ProfileScreen constructor and displaying it in a Text widget is the proper way.Step 3: Eliminate incorrect options
Global variables are discouraged; Navigator.pop sends data back, not forward.Final Answer:
Pass userId via ProfileScreen constructor and display it in a Text widget -> Option AQuick Check:
Push + constructor = pass data forward [OK]
- Using Navigator.pop to send data forward
- Relying on global variables for passing data
- Passing data as parameters to Navigator.pop
Ready to prove it?
Pass the test and this pattern moves unknown
