flutter 数据存储 SharedPreferences

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011272795/article/details/82710269

SharedPreferences

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用的一些常用配置。以键值对的形式保存设置,属性和数据。

在flutter平台也有一个SharedPreferences插件
在iOS上使用NSUserDefaults,在Android平台使用SharedPreferences,为简单数据提供持久存储。数据以异步方式持久保存到磁盘。

要使用此插件,请在pubspec.yaml文件中添加shared_preferences作为依赖项。
博主在写这篇文章时,最新版本为0.4.2。

引入


dependencies:
  flutter:
    sdk: flutter

  cupertino_icons: ^0.1.2
  shared_preferences: ^0.4.2

使用

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


  /*
   * 存储数据
   */
  Future _onClick() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.setString('account', account);
    print('存储acount为:$account');
  }

  /*
   * 读取数据
   */
  Future _readShared() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String account = preferences.get('account');
     print('读取到acount为:$account');
  }

  /*
   * 删除数据
   */
  Future _removeShared() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.remove('account');
    print('删除acount成功');
  }

因为是异步操作,所以async 和 await 一定不要省略。
如果想要读取的数据没有存储到磁盘中,则返回null。

然后我们写一个实际应用场景:页面有一 个输入框,3个按钮,分别把输入框中的内容存储到本地和从本地读取已经存了的数据以及删除我们存储的数据。在读取到数据时写入输入框。

看一下效果:

xiaoguo

奉上代码:

/*
 * Created by 李卓原 on 2018/9/13.
 * email: [email protected]
 *
 */

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

class MyInfoListPage extends StatelessWidget {
  TextEditingController accountController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        children: <Widget>[
          TextField(
            controller: accountController,
            decoration: InputDecoration(
              helperText: '请输入账号',
            ),
          ),
          RaisedButton(
            child: Text('存储'),
            onPressed: _onClick,
          ),
          RaisedButton(
            child: Text('读取'),
            onPressed: _readShared,
          ),
          RaisedButton(
            child: Text('删除'),
            onPressed: _removeShared,
          )
        ],
      ),
    );
  }

  /*
   * 存储数据
   */
  Future _onClick() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String account = accountController.text;
    preferences.setString('account', account);
    print('存储acount为:$account');
  }

  /*
   * 读取数据
   */
  Future _readShared() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    String account = preferences.get('account');
    print('读取到acount为:$account');
    accountController.text = account;
  }

  /*
   * 删除数据
   */
  Future _removeShared() async {
    SharedPreferences preferences = await SharedPreferences.getInstance();
    preferences.remove('account');
    print('删除acount成功');
  }
}


猜你喜欢

转载自blog.csdn.net/u011272795/article/details/82710269