json_navigation_saver 0.2.1 json_navigation_saver: ^0.2.1 copied to clipboard
This library is a helper library for converting routes with arguments to a string.
Flutter navigation saver library (json module) #
This library will help to restore navigation stack after application kill.
Overview #
This library should be used if you want to convert argument classes to string by using json serialization library. Our library has helper classes that will remove most of boilerplate code.
This library works best with shared prefreferences module
github link or pub link.
How to use this library: #
-
Include dependencies:
a.
build_value_navigation_saver: ^0.2.0
- current module that includes helper classes for build value integration.b.
shared_pref_navigation_saver: ^0.2.0
- module that will do actual navigation saving and restoring logic with core navigation saver library (github link or pub link).c.
json_annotation: ^3.0.0
- built value integration. for more information see this link -
Include dev dependencies:
a.
build_runner: ^1.0.0
- built value integration. for more information see this linkb.
json_serializable: ^3.2.0
- built value integration. for more information see this link -
Run
flutter pub upgrade
andflutter pub get
-
Initialize
NavigationSaver
class before your application widget:
import 'dart:convert';
import 'package:json_navigation_saver/json_navigation_saver.dart';
import 'package:shared_pref_navigation_saver/shared_pref_navigation_saver.dart';
import 'package:navigation_saver/navigation_saver.dart';
import 'serializers.dart';
void main() {
final NavigationSaver navigatorSaver = SharedPrefNavigationSaver(
(Iterable<RouteSettings> routes) async => json.encode(serializeRoutes(serializers, routes)),
(String routesAsString) async => deserializeRoutes(serializers, json.decode(routesAsString)),
);
runApp(MyApp(navigatorSaver));
}
- Setup
NavigationSaver
as navigation observer and make him generate widgets:
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, {
NextPageInfo nextPageInfo,
}) => /* todo: generate your application widgets here. use `settings`, not `routeSettings` object */,
),
navigatorObservers: [_navigationSaver],
);
}
}
- 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. - After that you should setup all your arguments with json serialization. See bellow code for instructions.
- Call
flutter packages pub run build_runner build
. - To see how to get result from restored routes see note in the core library: github or pub
How to use build value library #
- Add
JsonSerializable
annotation for you argument class. - Add this code bellow imports and above class definition:
import 'package:json_annotation/json_annotation.dart';
part 'argument_class_name_file_name.g.dart';
- Add additional code inside your class:
factory ArgumentClassName.fromJson(Map<String, dynamic> json) =>
_$ArgumentClassNameFromJson(json);
Map<String, dynamic> toJson() => _$ArgumentClassNameToJson(this);
- Do not forget to call
flutter packages pub run build_runner build
after each argument update.
See json serializable library for additional instructions.
For the complete example see an example application