Home > Blockchain >  Call a method on page pop
Call a method on page pop

Time:07-06

I'm pushing a new route like so in my app


Navigator.of(context)
          .push<void>(
        FilterTypesPage.routeFullScreen(context),
      ).then(
        (value) {
          log('PAGGGE POPPED');
        },
      ),

  static Route routeFullScreen(BuildContext context) {
    return MaterialPageRoute<void>(
        settings: const RouteSettings(name: routeName),
        builder: (_) => BlocProvider.value(
              value: BlocProvider.of<FeatureBloc>(context),
              child: const FilterTypesPage(),
            ),
        fullscreenDialog: true);
  }

for some reason log('PAGGGE POPPED'); doesn't get called on page close

I'd like to trigger a bloc event or a function when I close this page

CodePudding user response:

You should just call

Navigator.pop(context, someData);

from your RouteSettings where someData is the data you want to pass from the RouteSettings to the former page.

Then from your former page, you can perform your event handling inside the then block. The value inside the then block is the data that was passed from the RouteSettings page.

Alternatively, you can also use async-await instead of then in your former page.

onPressed: () async {
  final someData = await Navigator.of(cotext).push(.....);
  // Now perform your event handling which will be invoked after you pop `RouteSettings` page.
}
  • Related