GitHub 代码仓fluttercommunity/state_persistence

状态持久化

状态持久化 - 在应用启动之间保持状态。默认情况下,此库会将状态存储为应用程序数据目录中的本地 JSON 文件,名为 `data.json`。维护者:@slightfoot

状态持久化 项目封面
星标
75
复刻
2
最近推送(UTC)
2021年8月26日
项目状态
未归档

原始 README

以下为英文项目原文快照,最新内容请访问 GitHub。

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();
                }
              },
            ),
          ),
        ),
      ),
    );
  }
}
Flutter Community GitHub avatar
GitHub 作者

Flutter Community ↗

A central place for all community made Flutter packages. To get started, see the README of the 'community' repository.

语言DartSwiftKotlinObjective-C

分类

此仓库发布的插件

使用的依赖

依赖清单 4 项

  • path^1.8.0
  • path_provider^2.0.1
  • flutter{"sdk":"flutter"}
  • flutter_test开发依赖{"sdk":"flutter"}

如何选择适合项目的代码仓?

先按使用场景筛选,再比较项目活跃情况,最后到原始仓库核对使用要求。

  1. 通过仓库名、作者或描述搜索,再按技术分类缩小范围。
  2. 结合星标、复刻与最近推送比较项目;星标反映社区关注,不代表质量保证。
  3. 使用前在 GitHub 核对 README、许可证与 Issues。

来源与维护

“A repository is the most basic element of GitHub.”

GitHub Docs — About repositories ↗