We usually have such a requirement: click to modify user information to jump to the page, after the modification is completed, return to the previous page, we need to obtain new data to refresh the modified data. So I find a way to achieve, so far I have used the following three methods.

The code address

Navigator.of(context).pushNamed().then()

advantages

  • You can return to the page in thepopPass in parameter toNavigator.pop(context, 'data pass parameter ')..then(value => print(value))The value of value isData transmission and. Use gesture return to receive no parameter value printed asnull)
  • .thenMethod to listen for the gesture return andNavigator.popTo return.

disadvantages

  • Page jump on different pages more need in each onethenMethod, even if you write it as a public method, you have to add it once.

usage

Of (context). PushNamed ('/newView', arguments: newView (content: Then ((value) => print(value)); // Return to the previous page with Navigator. Pop (context, 'data pass '); Copy the codeCopy the code

Results show

deactivate

advantages

  • When we return to the page, we only need to request new data in one place, we don’t need to add it to each pagethenWithin the method, it can theoretically meet our needs.

disadvantages

  • althoughdeactivateWill be triggered, but will be triggered when entering or returning to the pageModalRoute.of(context).isCurrentCheck whether it is the current pagetrueSend a request for a new page. Is always returned when using gesturefalse, will result in no request to refresh the data.
  • Parameters cannot be obtained directly

usage

void deactivate() { super.deactivate(); var bool = ModalRoute.of(context).isCurrent; If (bool) {print(' return NewView '); }} Copy the codeCopy the code

Results show

didPopNext

advantages

  • didPopNextThis makes up for the fact that gesture returns don’t trigger, and we don’t need to write extra judgments.

disadvantages

  • Parameters cannot be obtained directly

usage

// Listens for class DynamicTheme extends StatefulWidget {const DynamicTheme(); static final RouteObserver<PageRoute> routeObserver = RouteObserver<PageRoute>(); @override _DynamicThemeState createState() => _DynamicThemeState(); } @override Widget build(BuildContext context) { return MaterialApp( title: 'Dynamic Theme', theme: lightTheme.copyWith(platform: _options.platform), darkTheme: darkTheme.copyWith(platform: _options.platform), themeMode: _options.themeMode, ... navigatorObservers: [DynamicTheme.routeObserver], ... routes: _buildRoutes(), ); } / / page using the import 'package: flutter/cupertino. Dart'; import 'package:flutter/material.dart'; class NewView extends StatefulWidget { final String content; const NewView({ this.content, }); static const String routeName = '/newView'; @override _NewViewState createState() => _NewViewState(); } class _NewViewState extends State<NewView> with RouteAware { @override void didChangeDependencies() { super.didChangeDependencies(); DynamicTheme.routeObserver.subscribe(this, ModalRoute.of(context)); } @override void didPopNext() {// Covering route was popped off the navigator.print (' return NewView'); } @override void didPush() {// Route was pushed onto navigator and is now topmost route. print(' enter NewView'); } @override void dispose() { DynamicTheme.routeObserver.unsubscribe(this); super.dispose(); } @override Widget build(BuildContext context) {// final NewView param = ModalRoute.of(context).settings.arguments; return CupertinoPageScaffold( navigationBar: CupertinoNavigationBar( leading: CupertinoButton( padding: EdgeInsets. Zero, child: Text(' return '), onPressed: () { // The demo is on the root navigator.// Navigator.of(context, rootNavigator: true).maybePop(); Navigator.pop(context, 'data pass '); }, ), ), child: Material( child: ScrollConfiguration( behavior: CustomBehavior(), child: ListView.builder( primary: true, itemCount: 60, itemBuilder: (BuildContext context, int index) { return Ink( child: InkWell( splashColor: Colors.transparent, onTap: () => Navigator.of(context).pushNamed( Detail.routeName, arguments: Detail(value: Container(height: 44.0, width: mediaQuery.of (context).size. Width, child: Center(child: Text('Data-$index')), ), ), ); },),),),); }} Copy the codeCopy the code

Results show

conclusion

If you want to listen for the page to go back and get new data, you only need to listen for one jump using the then method, but if you have a lot of jumps and you need to refresh the data after you return, didPopNext is recommended.