Skip to content

Instantly share code, notes, and snippets.

@mingsai
Last active August 28, 2021 01:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mingsai/01cecc3abd19f603917da40be18dce37 to your computer and use it in GitHub Desktop.
Save mingsai/01cecc3abd19f603917da40be18dce37 to your computer and use it in GitHub Desktop.
Flutter - How to pass an object using the Navigator between two screens
//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