Search categories, packages and repositories

Package detailsv0.1.0

state_persistence

Persist state across app launches. By default this library store state as a local JSON file called `data.json` in the applications data directory.

AndroidiOSmacOSWindowsLinux
30-day downloads
40
Likes
65
Pub points
140

Install package

flutter pub add state_persistence

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

The state_persistence package enables persistent storage of application state across app launches in Flutter. It automatically saves state to a local JSON file named `data.json` in the app's data directory, ensuring data survives restarts and device reboots. The library is designed for simplicity, offering an easy-to-use API that integrates seamlessly with state management solutions. It supports multiple platforms including Android, iOS, macOS, Windows, and Linux, making it ideal for cross-platform apps requiring reliable state retention.

Use cases

  • Saving user preferences between sessions
  • Persisting form data during app restarts
  • Maintaining UI state after app close
  • Storing cached data for offline use
  • Enabling seamless app resumption

Key features

  • Automatic JSON file persistence
  • Cross-platform support
  • Simple API integration
  • Configurable file path and name
  • Lightweight and fast serialization

Suitable for

  • Apps needing long-term state retention
  • Developers using simple state management
  • Projects requiring minimal setup for persistence
  • Offline-capable applications
  • Prototypes and MVPs

Considerations

  • File size may grow over time if not managed
  • Sensitive data should be encrypted before saving
  • Not suitable for large datasets without optimization
  • Requires manual handling of schema changes
  • Limited to JSON format by default

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

Flutter Community: state_persistence

State Persistence

pub package

Persist state across app launches. By default this library store state as a local JSON file called data.json in the applications data directory. You can change this filename by providing another storage mechanism.

If you do not want to store your persisted app state as a JSON file you can extend PersistedStateStorage and provide your own methods for saving and loading data. E.g shared_preferences or sqflite. For those of you that are ambitious you could even store your state on the web or even in Firebase.

To change the persisted state simply modify the values in the data map. These changes will automatically be persisted to disk based on the saveTimeout given to the PersistedAppState widget. By default this value is 500 milliseconds. This timeout is used to stop disk thrashing on multiple map changes in quick succession.

Example
import 'package:flutter/material.dart';
import 'package:state_persistence/state_persistence.dart';

void main() => runApp(App());

class App extends StatefulWidget {
  @override
  _AppState createState() => _AppState();
}

class _AppState extends State<App> {
  TextEditingController _textController;

  @override
  Widget build(BuildContext context) {
    return PersistedAppState(
      storage: JsonFileStorage(),
      child: MaterialApp(
        title: 'Persistent TextField Example',
        theme: ThemeData(primarySwatch: Colors.indigo),
        home: Scaffold(
          appBar: AppBar(title: Text('Persistent TextField Example')),
          body: Container(
            padding: const EdgeInsets.all(32.0),
            alignment: Alignment.center,
            child: PersistedStateBuilder(
              builder: (BuildContext context, AsyncSnapshot<PersistedData> snapshot) {
                if (snapshot.hasData) {
                  if (_textController == null) {
                    _textController = TextEditingController(text: snapshot.data['text'] ?? '');
                  }
                  return TextField(
                    controller: _textController,
                    decoration: InputDecoration(
                      hintText: 'Enter some text',
                    ),
                    onChanged: (String value) => snapshot.data['text'] = value,
                  );
                } else {
                  return CircularProgressIndicator();
                }
              },
            ),
          ),
        ),
      ),
    );
  }
}

Categories

Dependencies

Dependency list 2 items

Source repository

Projects using this package

No data available yet.