Bird
Raised Fist0
Fluttermobile~20 mins

Passing data between screens in Flutter - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Data Passing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ ui_behavior
intermediate
2:00remaining
Passing data with Navigator.push
What will be displayed on the second screen after navigating from the first screen with the given code?
Flutter
class FirstScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.push(
          context,
          MaterialPageRoute(
            builder: (context) => SecondScreen(data: 'Hello'),
          ),
        );
      },
      child: Text('Go'),
    );
  }
}

class SecondScreen extends StatelessWidget {
  final String data;
  SecondScreen({required this.data});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(child: Text(data)),
    );
  }
}
AThe second screen shows a blank screen
BThe app crashes with a runtime error
CThe second screen shows a centered text: Hello
DThe second screen shows the text: Go
Attempts:
2 left
💡 Hint
Check how the data is passed through the constructor and displayed in the Text widget.
🧠 Conceptual
intermediate
2:00remaining
Understanding Navigator.pop with data
What value will be received in the first screen after popping the second screen with Navigator.pop(context, 42)?
Flutter
class FirstScreen extends StatefulWidget {
  @override
  State<FirstScreen> createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  int? result;

  void _navigate() async {
    final data = await Navigator.push<int>(
      context,
      MaterialPageRoute(builder: (context) => SecondScreen()),
    );
    setState(() {
      result = data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        ElevatedButton(onPressed: _navigate, child: Text('Go')),
        if (result != null) Text('Result: $result'),
      ],
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.pop(context, 42);
      },
      child: Text('Return 42'),
    );
  }
}
AThe first screen shows the text: Result: 42
BThe first screen shows no text
CThe app crashes with a type error
DThe first screen shows the text: Result: null
Attempts:
2 left
💡 Hint
Navigator.pop can send data back to the previous screen using the second argument.
❓ lifecycle
advanced
2:00remaining
State retention when passing data between screens
Given the code below, what will be the value of counter on the first screen after returning from the second screen?
Flutter
class FirstScreen extends StatefulWidget {
  @override
  State<FirstScreen> createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  int counter = 0;

  void _navigate() async {
    final increment = await Navigator.push<int>(
      context,
      MaterialPageRoute(builder: (context) => SecondScreen()),
    );
    if (increment != null) {
      setState(() {
        counter += increment;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text('Counter: $counter'),
        ElevatedButton(onPressed: _navigate, child: Text('Go')),
      ],
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.pop(context, 5);
      },
      child: Text('Add 5'),
    );
  }
}
AThe first screen shows: Counter: null
BThe first screen shows: Counter: 0
CThe app crashes with a runtime error
DThe first screen shows: Counter: 5
Attempts:
2 left
💡 Hint
The first screen updates its state with the value returned from the second screen.
📝 Syntax
advanced
2:00remaining
Correct way to pass multiple data items between screens
Which option correctly passes two strings, name and email, from FirstScreen to SecondScreen?
Flutter
class SecondScreen extends StatelessWidget {
  final String name;
  final String email;
  SecondScreen({required this.name, required this.email});

  @override
  Widget build(BuildContext context) {
    return Text('$name - $email');
  }
}

// In FirstScreen, which code is correct to navigate?
ANavigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen('Alice', 'alice@example.com')));
BNavigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen(name: 'Alice', email: 'alice@example.com')));
CNavigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen(name: 'Alice')));
DNavigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen(email: 'alice@example.com')));
Attempts:
2 left
💡 Hint
Check the constructor parameters and how named arguments are passed in Dart.
🔧 Debug
expert
2:00remaining
Why does data not update after returning from second screen?
Given the code below, why does the first screen not show the updated data after returning from the second screen?
Flutter
class FirstScreen extends StatefulWidget {
  @override
  State<FirstScreen> createState() => _FirstScreenState();
}

class _FirstScreenState extends State<FirstScreen> {
  String data = 'Old';

  void _navigate() {
    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => SecondScreen()),
    ).then((value) {
      data = value ?? data;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Column(
      children: [
        Text(data),
        ElevatedButton(onPressed: _navigate, child: Text('Go')),
      ],
    );
  }
}

class SecondScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return ElevatedButton(
      onPressed: () {
        Navigator.pop(context, 'New');
      },
      child: Text('Return New'),
    );
  }
}
ABecause setState is not called after updating data, so UI does not refresh
BBecause Navigator.pop does not send data back correctly
CBecause the initial value of data is final and cannot be changed
DBecause the Text widget cannot display variables
Attempts:
2 left
💡 Hint
Remember how Flutter updates the UI when state changes.

Practice

(1/5)
1. What is the main purpose of passing data between screens in a Flutter app?
easy
A. To change the app's theme colors dynamically
B. To store data permanently on the device
C. To improve the app's startup speed
D. To share information as the user moves from one screen to another

Solution

  1. Step 1: Understand screen navigation

    When users move between screens, apps often need to share information to keep context.
  2. Step 2: Identify the role of data passing

    Passing data allows the next screen to display or use information from the previous screen.
  3. Final Answer:

    To share information as the user moves from one screen to another -> Option D
  4. Quick Check:

    Passing data = share info between screens [OK]
Hint: Passing data shares info between screens during navigation [OK]
Common Mistakes:
  • Confusing data passing with theme or style changes
  • Thinking data passing stores data permanently
  • Assuming data passing speeds up app startup
2. Which Flutter code snippet correctly passes a string "Hello" to a new screen called DetailScreen?
easy
A. Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen("Hello")));
B. Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen));
C. Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello")));
D. Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen.data("Hello")));

Solution

  1. Step 1: Check constructor usage

    Passing data requires calling the screen's constructor with named or positional parameters.
  2. 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.
  3. Final Answer:

    Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello"))); -> Option C
  4. Quick Check:

    Correct constructor call = Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen(data: "Hello"))); [OK]
Hint: Use constructor with named params inside MaterialPageRoute builder [OK]
Common Mistakes:
  • Omitting parentheses after DetailScreen
  • Passing data without parameter name when required
  • Using incorrect method calls on DetailScreen
3. Given this code, what will be displayed on the 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")));
medium
A. An empty screen
B. Text widget showing "Welcome"
C. Error because message is not passed
D. Text widget showing "message" literally

Solution

  1. Step 1: Analyze constructor parameter

    DetailScreen requires a String message passed via constructor, which is done correctly with "Welcome".
  2. Step 2: Check build method output

    The build method shows a Text widget displaying the message variable, so it will show "Welcome".
  3. Final Answer:

    Text widget showing "Welcome" -> Option B
  4. Quick Check:

    Passed message = displayed text [OK]
Hint: Passed constructor data shows in Text widget [OK]
Common Mistakes:
  • Assuming message is not passed causes error
  • Thinking Text shows variable name instead of value
  • Ignoring the required keyword in constructor
4. What is wrong with this code snippet for passing data?
Navigator.push(context, MaterialPageRoute(builder: (context) => DetailScreen));
medium
A. DetailScreen is missing parentheses to call its constructor
B. Navigator.push cannot be used for navigation
C. MaterialPageRoute should not have a builder
D. Context is not needed in Navigator.push

Solution

  1. Step 1: Check DetailScreen usage

    DetailScreen is a class and must be instantiated by calling its constructor with parentheses.
  2. Step 2: Identify missing constructor call

    Without parentheses, DetailScreen is a type, not an instance, causing a syntax error.
  3. Final Answer:

    DetailScreen is missing parentheses to call its constructor -> Option A
  4. Quick Check:

    Constructor call needs parentheses [OK]
Hint: Always add () to call a widget constructor [OK]
Common Mistakes:
  • Forgetting parentheses after widget class name
  • Misunderstanding Navigator.push usage
  • Thinking context is optional in navigation
5. You want to pass a user ID (integer) from HomeScreen to ProfileScreen and display it. Which approach correctly passes and uses this data?
hard
A. Pass userId via ProfileScreen constructor and display it in a Text widget
B. Store userId in a global variable and access it directly in ProfileScreen
C. Use Navigator.pop to send userId to ProfileScreen
D. Pass userId as a parameter to Navigator.pop

Solution

  1. Step 1: Understand data passing direction

    Navigator.push sends data forward to a new screen via constructor parameters.
  2. Step 2: Identify correct data usage

    Passing userId via ProfileScreen constructor and displaying it in a Text widget is the proper way.
  3. Step 3: Eliminate incorrect options

    Global variables are discouraged; Navigator.pop sends data back, not forward.
  4. Final Answer:

    Pass userId via ProfileScreen constructor and display it in a Text widget -> Option A
  5. Quick Check:

    Push + constructor = pass data forward [OK]
Hint: Use constructor params to pass data forward between screens [OK]
Common Mistakes:
  • Using Navigator.pop to send data forward
  • Relying on global variables for passing data
  • Passing data as parameters to Navigator.pop
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard