Unlike other routing solutions, AutoRoute can pass any type of argument to it's routes! AutoRoute automatically detects and handles your page arguments for you, and the generated Route object will deliver all the arguments your page needs including callback functions (eg. to return results).
The following page widget will take an argument of typeBook
and a callback function that returns a rating value on pop.class BookDetailsPage extends StatelessWidget {
BookDetailsPage({@PathParam('bookId') this.bookId, this.onRateBook});
final int bookId;
final void Function(int) onRateBook;
...
BookDetailsRoute
will deliver the same arguments to it's corresponding pagecontext.pushRoute(
BookDetailsRoute(
bookId: 1,
onRateBook: (rating) {
// handle result
}),
);
onRateBook?.call(RESULT);
context.router.pop();
If you would prefer to await
for the results instead of using a callback like above, you can do the following:
// In your router declaration, define the return type
AutoRoute<int>(page: BookDetailsPage)
class BookDetailsPage extends StatelessWidget {
BookDetailsPage({@PathParam('bookId') this.bookId});
// note that we took out the onRateBook callback
final int bookId;
// In the BookDetailsPage, pop with the values you want to return
onPressed: () async {
context.popRoute(<BOOK_RATING>)
}
onPressed: () async {
final int value = await context.pushRoute(BookDetailsPage(bookId: 1))
doSomethingWithYourValue(value)
}