ui_state_persist 0.0.1

  • README.md
  • Example
  • Installing
  • Versions
  • new44


When mobile devices are out of memory, they tell apps to save their state, then kill them. This means Flutter apps can sometimes (or often, for low-memory devices) go back to the beginning when multi-tasking. Flutter doesn't expose the Android or iOS "native" ways of restoring state (see issue here). This library tries to provide a Dart-only solution, with some limitations.

How to use this library

Load the state

UIState is a singleton class. This means you can access it anywhere with UIState(). You need to load it first:

void  main() async {
    await  UIState().load();

If anyone can think of a better way to access/setup this class that is more testing-friendly or less "global", please open an issue/let me know!


This is a bit awkward. It should be better once named routes can be used with parameters. You must set up routing like this:

Widget build(BuildContext context) {
    return MaterialApp(
        title: 'ui_state_persist example',
        //make sure to set the initialRoute
        initialRoute: UIState().route,
        //note there is no "home" or anything else for the routes, just onGenerateRoute
        onGenerateRoute: (routeSettings) {
            //Route name is set here but not cleared, because of arguments.
            //This means is won't get cleared when restoring to a deeply-nested
            //route and pressing/swiping 'back'
            UIState().route = routeSettings.name;
            switch (routeSettings.name) {
            case "/":
                return MaterialPageRoute(
                	builder: (context) => MyHomePage(title: 'ui_state_persist example')
            case "/view":
                return MaterialPageRoute(
                	builder: (context) => ViewPage(Entry.fromJson(UIState().routeArgument))

Then when you want to go to a different page:

  onPressed: ()  {
    //clear first
    // then set the routeArgument (null if not needed)
    UIState().routeArgument = Entry(
      index: i + _counter,
      color: HSVColor.fromAHSV(1.0, i/100 * 360, 1.0, 1.0).toColor(),
    //then navigate using pushNamed


Controllers, animations, FocusNodes, etc. are all listenables in flutter. Right now only ScrollController, TextEditingController and ValueNotifier are supported. It's easy to add more, which I will be doing, or pull requests are welcome.

To use a listenable, follow this example in your build method:

	controller: UIState().useListenable<TextEditingController>("textedit1"),
	decoration: InputDecoration(labelText: "Comments"),

Tip: you can use ValueNotifier to wrap a variable and it will be auto-magically persisted.


I plan on adding a useStream(String key, Stream stream) function to use with BLoCs easily.

Manually persisting variables

Use getRaw<T>(String key) to manually get a variable, but it won't be updated unless you call setRaw(String key, dynamic value).

Using with Custom Classes

This library uses jsonEncode and jsonDecode. This means you can use it with your own models/classes - just implement toJson() and fromJson(). Note that loading a custom class will return a Map<String, dynamic>, so you have to pass it to your toJson().


class Entry {
  final int index;
  final Color color;
  Entry({this.index, this.color});
  Entry.fromJson(Map<String, dynamic> json) :
    index = json["index"],
    color = Color(json["color"]);
  Map<String, dynamic> toJson() => {
    "index": index,
    "color": color.value,

Using in a MaterialPageRoute:

return MaterialPageRoute(
  builder: (context) => ViewPage(Entry.fromJson(UIState().routeArgument))

Setting the routeArgument. Note the use of toJson()

UIState().routeArgument = Entry(
  index: i + _counter,
  color: HSVColor.fromAHSV(1.0, i/100 * 360, 1.0, 1.0).toColor(),


  • This is really just an experiment. Use at your own risk
  • It's a Very Bad Idea to use this to manage your App State, since it only does one page at a time.
  • Only the routeArgument to the top route is preserved, and the top route's UI State. I will probably fix this first
  • No tests yet

[0.0.1] - 30/11/2018

  • Initial release.



A new Flutter project.

Getting Started

For help getting started with Flutter, view our online documentation.

Use this package as a library

1. Depend on it

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

  ui_state_persist: ^0.0.1

2. Install it

You can install packages from the command line:

with Flutter:

$ flutter packages get

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

3. Import it

Now in your Dart code, you can use:

import 'package:ui_state_persist/ui_state_persist.dart';
Version Uploaded Documentation Archive
0.0.1 Nov 29, 2018 Go to the documentation of ui_state_persist 0.0.1 Download ui_state_persist 0.0.1 archive
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
Learn more about scoring.

We analyzed this package on Dec 5, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.1.0
  • pana: 0.12.7
  • Flutter: 1.0.0


Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

The description is too short. (-20 points)

Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

Package is pre-v0.1 release. (-10 points)

While there is nothing inherently wrong with versions of 0.0.*, it usually means that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev.68.0 <3.0.0
flutter 0.0.0
path ^1.6.2 1.6.2
path_provider ^0.4.1 0.4.1
Transitive dependencies
collection 1.14.11
meta 1.1.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies