https://github.com/fluttercommunity/state_persistence
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. Maintainer: @slightfoot
https://github.com/fluttercommunity/state_persistence
hacktoberfest
Last synced: about 1 year ago
JSON representation
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. Maintainer: @slightfoot
- Host: GitHub
- URL: https://github.com/fluttercommunity/state_persistence
- Owner: fluttercommunity
- License: mit
- Created: 2019-04-04T23:16:20.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2021-08-26T07:42:03.000Z (almost 5 years ago)
- Last Synced: 2025-03-28T15:51:58.541Z (about 1 year ago)
- Topics: hacktoberfest
- Language: Dart
- Homepage: https://pub.dev/packages/state_persistence
- Size: 81.1 KB
- Stars: 74
- Watchers: 4
- Forks: 2
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/fluttercommunity/community)
# State Persistence
[](https://pub.dartlang.org/packages/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. 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
```dart
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 {
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 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();
}
},
),
),
),
),
);
}
}
```