Giter VIP home page Giter VIP logo

flutter_persistent_value_notifier's Introduction

The flutter_persistent_value_notifier library

ReactiveValue resets to its initial value every time the app is restarted. You can persist values across app restarts by using PersistentValueNotifier rather than ValueNotifier.

This Flutter library extends ValueNotifier<T> to provide a PersistentValueNotifier that stores the ValueNotifier's value in SharedPreferences, so that changes in the value survive app restarts.

Usage

(1) Add a dependency upon flutter_persistent_value_notifier in your pubspec.yaml (replace any with the latest version, if you want to control the version), then run flutter pub get:

dependencies:
  flutter:
    sdk: flutter
  flutter_persistent_value_notifier: any

(2) Import the package in your Flutter project:

import 'package:flutter_persistent_value_notifier/'
            'flutter_persistent_value_notifier.dart';

(3) In your async main method, initialize WidgetsFlutterBinding, then initialize the persistent_value_notifier library by calling await initPersistentValueNotifier(), which starts SharedPreferences and loads any persisted values from the SharedPreferences backing store.

void main() async {
  // Both of the following lines are needed, in this order
  WidgetsFlutterBinding.ensureInitialized();
  await initPersistentValueNotifier(prefix: 'com.myapp.');  // A unique SharedPreferences prefix for your app

  runApp(MainPage());
}

(4) Use PersistentValueNotifier in place of ValueNotifier in your code, as shown below.

final counter = PersistentValueNotifier<int>(
  sharedPreferencesKey: 'counter',
  initialValue: 0,
);

counter.value will be set to the initial value of 0 if it has never been set before, but if it has been set before in a previous run of the app, the previous value will be recovered from SharedPreferences instead, using the shared preferences key 'counter'.

Whenever counter.value is set in future, not only is the underlying ValueNotifier's value updated, but the new value is asynchronously written through to SharedPreferences, using the same key.

Variants

You can also use PersistentValueNotifierEnum to persistently store enum values:

enum Fruit { apple, pair, banana };

final fruit = PersistentValueNotifierEnum<Fruit>(
  sharedPreferencesKey: 'fruit',
  initialValue: Fruit.apple,
  nameToValueMap: Fruit.values.asNameMap(),
);

Or you can use PersistentValueNotifierJsonEncoded to persistently store arbitrary JSON-serializable classes:

final fruit = PersistentValueNotifierJsonEncoded<UserProfile?>(
  sharedPreferencesKey: 'user-profile',
  initialValue: null,
  toJson: (u) => jsonEncode(u?.toJson()),
  fromJson: (jsonStr) => UserProfile.fromJson(jsonDecode(jsonStr)),
);

Pro-tip

See also my other library, flutter_reactive_value, as an easy way to add reactive state to your app!

Author

flutter_persistent_value_notifier was written by Luke Hutchison, and is released under the MIT license.

flutter_persistent_value_notifier's People

Contributors

lukehutch avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

flutter_persistent_value_notifier's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.