json_navigation_saver 0.2.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 63

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.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

  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.2.1] - 22.11.2019 #

  • Read.me update + library update

[0.2.0+1] - 22.11.2019 #

  • Read.me update

[0.2.0] - 22.11.2019 #

  • Read.me update

[0.0.1] - 22.11.2019 #

  • Initial release.

example/lib/main.dart

import 'dart:convert';

import 'package:example/home/home_page.dart';
import 'package:example/home/home_page_arguments.dart';
import 'package:flutter/material.dart';
import 'package:json_navigation_saver/json_navigation_saver.dart';
import 'package:navigation_saver/navigation_saver.dart';
import 'package:shared_pref_navigation_saver/shared_pref_navigation_saver.dart';

void main() {
  final NavigationSaver _navigatorSaver = SharedPrefNavigationSaver(
    (Iterable<RouteSettings> routes) async =>
        json.encode(serializeRoutes(routes)),
    (String routesAsString) async =>
        deserializeRoutes(json.decode(routesAsString)),
  );

  runApp(MyApp(_navigatorSaver));
}

class MyApp extends StatelessWidget {
  MyApp(this._navigationSaver);

  final NavigationSaver _navigationSaver;

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: NavigationSaver.restoreRouteName,
      onGenerateRoute: (RouteSettings routeSettings) =>
          _navigationSaver.onGenerateRoute(
        routeSettings,
        (
          RouteSettings settings, {
          NextPageInfo nextPageInfo,
        }) =>
            MaterialPageRoute(
          builder: (BuildContext context) => MyHomePage(
            initialCounter: settings.arguments is Map
                ? MyHomePageArguments.fromJson(
                        settings.arguments as Map<String, dynamic>)
                    .deepIndex
                : 0,
            nextPageInfo: nextPageInfo,
          ),
          settings: routeSettings,
        ),
      ),
      navigatorObservers: [_navigationSaver],
    );
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  json_navigation_saver: ^0.2.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:json_navigation_saver/json_navigation_saver.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
27
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
63
Learn more about scoring.

We analyzed this package on Mar 27, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6
  • Flutter: 1.12.13+hotfix.8

Health issues and suggestions

Document public APIs. (-1 points)

21 out of 21 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
json_annotation ^3.0.0 3.0.1
navigation_saver ^0.2.0 0.2.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
build_runner ^1.0.0
flutter_test
json_serializable ^3.2.0
pedantic ^1.0.0 1.9.0