Last active
August 28, 2021 01:10
-
-
Save mingsai/01cecc3abd19f603917da40be18dce37 to your computer and use it in GitHub Desktop.
Flutter - How to pass an object using the Navigator between two screens
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//View #1 Pass any object as an argument in the Navigator as part of a Map: | |
void _editItem(BuildContext context, int index, Box<ChecklistItem> box) { | |
ChecklistItem _selectedItem = box.getAt(index) as ChecklistItem; | |
Navigator.pushNamed(context, ChecklistEditScreen.id, | |
arguments: {'item': _selectedItem}); | |
} | |
//View #2 Extract passed flutter arguments without passing them as parameters: | |
Widget build(BuildContext context) { | |
Map? args = ModalRoute.of(context)?.settings.arguments as Map?; | |
if (args == null) return Container(); | |
ChecklistItem _item = args['item'] as ChecklistItem; | |
return ChecklistEditContent(item: _item); | |
} | |
//Usage: | |
onDoubleTap: () => _editItem(context, index, box), |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment