shared_pref_navigation_saver 0.3.3 shared_pref_navigation_saver: ^0.3.3 copied to clipboard
This library will help to restore navigation stack after application kill.
Flutter navigation saver library (shared preferences module) #
This library will help to restore navigation stack after application kill.
Overview #
This library should be used if you have some special way of serializing arguments into the string and back. Also do not forget to check general readme.
How does this module work: #
- It uses the core module (git link or pub link) and extends it to save routes to the shared preferences.
- All the control of converting routes to string is up to the application user code.
maximumDurationBetweenRestoration
is used to understand if we need to restore routes or too much time was passed. Default is 5 minutes.
General implementation: #
- Include dependencies:
shared_pref_navigation_saver: ^0.3.3
- current module - Include any argument to disk saving library or write it by yourself. We suggest
build value
(git hub or pub link) orjson module
(git hub or pub link). - Create
NavigationSaver
class before your application widget:
import 'package:build_value_navigation_saver/navigation_saver_routes_info.dart';
import 'package:shared_pref_navigation_saver/shared_pref_navigation_saver.dart';
void main() {
final NavigationSaver _navigatorSaver = SharedPrefNavigationSaver(
(Iterable<RouteSettings> routes) async => /* todo: add save code */,
(String routesAsString) async => /* todo: add restore code */,
);
runApp(MyApp(_navigatorSaver));
}
- Setup
NavigationSaver
as navigation observer and route generator:
class MyApp extends StatelessWidget {
MyApp(this._navigationSaver);
final NavigationSaver _navigationSaver;
@override
Widget build(BuildContext context) {
return MaterialApp(
initialRoute: NavigationSaver.restoreRouteName,
onGenerateRoute: (RouteSettings routeSettings) => _navigationSaver.onGenerateRoute(
routeSettings,
(
RouteSettings settings,
String routeName,
Object routeArguments, {
NextPageInfo nextPageInfo,
}) => /* todo: generate your application widgets here. use `routeName` and `routeArguments` */,
),
navigatorObservers: [_navigationSaver],
);
}
}
- This is it. Also you may want to add custom restoration widget that will be shown when library restore your navigation stack. This can be done by passing
restoreRouteWidgetBuilder
parameter inonGenerateRoute
method. - You may want to add custom restoration widget that will be shown when library restore your navigation stack. This can be done by passing
restoreRouteWidgetBuilder
parameter inonGenerateRoute
method. - To see how to get result from restored routes see note in the core library: github or pub
Please check built value module
(githib link or pub link) or json module
(github link or pub link) for argument to map converting logic.