json_navigation_saver 0.3.3

Flutter Android iOS

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: #

  1. Include dependencies:

    a. build_value_navigation_saver: ^0.3.3 - current module that includes helper classes for build value integration.

    b. shared_pref_navigation_saver: ^0.3.3 - 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

  2. Include dev dependencies:

    a. build_runner: ^1.0.0 - built value integration. for more information see this link

    b. json_serializable: ^3.2.0 - built value integration. for more information see this link

  3. Run flutter pub upgrade and flutter pub get

  4. 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));
}

  1. 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],
    );
  }
}
  1. 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 in onGenerateRoute method.
  2. After that you should setup all your arguments with json serialization. See bellow code for instructions.
  3. Call flutter packages pub run build_runner build.
  4. To see how to get result from restored routes see note in the core library: github or pub

How to use build value library #

  1. Add JsonSerializable annotation for you argument class.
  2. Add this code bellow imports and above class definition:
import 'package:json_annotation/json_annotation.dart';
        
part 'argument_class_name_file_name.g.dart';
  1. Add additional code inside your class:
  factory ArgumentClassName.fromJson(Map<String, dynamic> json) =>
      _$ArgumentClassNameFromJson(json);

  Map<String, dynamic> toJson() => _$ArgumentClassNameToJson(this);
  1. 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

0
likes
90
pub points
27%
popularity

Publisher

scal.io

This library is a helper library for converting routes with arguments to a string.

Repository (GitHub)
View/report issues

Documentation

Documentation
API reference

License

BSD (LICENSE)

Dependencies

flutter, json_annotation, navigation_saver

More

Packages that depend on json_navigation_saver