0
0
Fluttermobile~20 mins

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

Choose your learning style9 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.