Home > Back-end >  how to send object when i pop? (flutter)
how to send object when i pop? (flutter)

Time:01-23

Context: I'm creating an app for sales and when I'm on the sales screen I have a button (Add item), this button takes me to the items grid.

my difficulty is turning on the sales screen with the selected item.

onTap: () => isSelecting ? Navigator.of(context).pop() : null,

CodePudding user response:

in the previous screen you can catch this object as a parameter in the pop method.

final result = await Navigator.push(
context,
MaterialPageRoute(builder: (context) => NextScreen()),
);

In Flutter, you can use the Navigator.pop method to pop a route off the stack and return to the previous route. To send an object back to the previous route, you can pass it as an argument to the pop method.

Here's an example of how you might use the pop method to send an object back to the previous route:

Widget build(BuildContext context) {
return Scaffold(
  body: Center(
    child: RaisedButton(
      child: Text('Go back'),
      onPressed: () {
        final myObject = "Hello from next screen";
        Navigator.pop(context, myObject);
      },
    ),
  ),
);

}

CodePudding user response:

onPressed: () async { newProduto = await Navigator.of(context).push(MaterialPageRoute(builder: (context) => ProdutosPage(selecting: true))); setState(() { produtos.add(newProduto); }); }

  • Related