W
W
Wynell_ru2019-11-02 22:41:42
Dart
Wynell_ru, 2019-11-02 22:41:42

How to handle back to previous page in Flutter?

How to handle back to previous page in Flutter?
That is, I used Navigator.push, and how to perform certain actions exactly at the moment when the user closes this second page / goes back ?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KnightForce, 2020-02-25
@KnightForce

async () {
    await Navigator.push() // Предыдущий экран будет ждать результат роута
}

You can return something from the screen:
async () {
  final int result =  await Navigator.push() // Предыдущий экран будет ждать результат роута
  
  // Где-то на другом экране:
 
  navigator.pop(1);

}

R
Ruslan, 2020-05-17
@Razbezhkin

Here is how I solved such a problem for a scenario where the user presses the back arrow on the device itself so that a certain page can catch the very moment when the page is retrieved from the history and shown to the user.
I added the "RouteAware" mixin to the class that is the page and overloaded the didPopNext method.
here is an example.

//страница
class CartPage extends StatefulWidget {
  @override
  _CartPageState createState() {
    return _CartPageState();
  }
}

//состояние страницы
class _CartPageState extends State<CartPage> with RouteAware {
    //вызывается, когда на эту страницу происходит возврат.
  @override
  void didPopNext() {
    _loadCart();
  }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question