Search⌘ K
AI Features

Solution: Navigation and Routing

Explore practical solutions for navigating Flutter apps. Learn to return data between screens, display it in SnackBars, and clear the navigation stack to improve app flow and user experience.

Solutions

Great job on completing all the steps in the previous challenge! Feel free to compare your code solutions with the solutions below:

Dart
IconButton(
onPressed: () {
// TODO-1: Return data from screen below
// solution
Navigator.pop(context, shopItem.itemName);
},
icon: const Icon(
Icons.arrow_back,
size: 32,
),
),
GestureDetector(
// TODO-2: Display data received
// solution
onTap: () async{
var data = await Navigator.pushNamed(context, itemDetailRoute,arguments: shopList[index]);
ScaffoldMessenger.of(context)
..removeCurrentSnackBar()
..showSnackBar(SnackBar(content: Text('$data')));
},
child: ItemCard(
shopItem: shopList[index],
),
),
ElevatedButton(
onPressed: () {
// TODO-3: Clear routes to first screen
// solution
Navigator.popUntil(context, (route)=> route.isFirst);
},
...
)
...