json_navigation_saver 0.2.0+1 json_navigation_saver: ^0.2.0+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 bollerplate code. This library works best with shared prefreferences module
How to use this library: #
-
Include dependencies:
a.
build_value_navigation_saver: ^0.2.0
- current module that willb.
shared_pref_navigation_saver: ^0.2.0
- module that will do actial navigation saving and restoring logicc.
built_value: ^6.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.
built_value_generator: ^6.0.0
- built value integration. for more information see this link -
Create
NavigationSaver
class before your application widget:
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));
}
- Create
serializers.dart
file near themain.dart
file with the following code:
import 'package:build_value_navigation_saver/navigation_saver_routes_info.dart';
import 'package:built_value/serializer.dart';
import 'package:built_collection/built_collection.dart';
part 'serializers.g.dart';
/// Example of how to use built_value serialization.
///
/// Declare a top level [Serializers] field called serializers. Annotate it
/// with [SerializersFor] and provide a `const` `List` of types you want to
/// be serializable.
///
/// The built_value code generator will provide the implementation. It will
/// contain serializers for all the types asked for explicitly plus all the
/// types needed transitively via fields.
///
/// You usually only need to do this once per project.
@SerializersFor([
RoutesInfo,
NavigatorRouteSettings,
/* todo: add all other Arguments that you pass to your routes */
])
final Serializers serializers = (_$serializers.toBuilder()).build();
- Call
flutter packages pub run build_runner build
. If you do everything correctly it will buildserializers.g.dart
file and all missed imports will be gone. If something goes wrong see the build value documentation - 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` */,
),
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
paramter toonGenerateRoute
method.
For the complete example see an example application