我爱模板网 > 建站教程 > APP开发,混合APP >  Flutter笔记66:本地存储shared_preferences的简单使用正文

Flutter笔记66:本地存储shared_preferences的简单使用

Flutter本地存储比较常用的插件shared_preferences,它其实就是对IOS的NSUserDefaults和安卓的SharedPreferences的分别实现,这里是简单用法示例:

1、在pubspec.yaml中安装
shared_preferences: ^0.5.12+2
2、读取方法如:
void initFromCache() async{
    final SharedPreferences prefs = await _prefs;
    final value_nickname = prefs.getString('key_nickname');
    final value_dart = prefs.getBool('key_dart');
}
3、写入方法如:
void saveInfo(String value_nickname) async{
    final SharedPreferences prefs = await _prefs;
    prefs.setString('key_nickname', value_nickname);
    prefs.setBool('key_dart',  this.value_dart);
}
4、完整代码如下:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Container(
      child: MaterialApp(
        title: 'shared_preferences本地存储',
        debugShowCheckedModeBanner: false,
        home: FirstPage(),
      ),
    );
  }
}

class FirstPage extends StatefulWidget{
  FirstPage({Key key}) : super(key:key);

  _FirstPageState createState() => _FirstPageState();
}

class _FirstPageState extends State<FirstPage>{
  //实例化SharedPreferences,是异步处理
  Future<SharedPreferences> _prefs = SharedPreferences.getInstance();

  var controller = TextEditingController();
  bool value_dart = false;
  bool value_js = false;
  bool value_java = false;

  @override
  void initState() {
    super.initState();
    //从缓存中取出数据
    initFromCache();
  }

  //从缓存中取值
  void initFromCache() async{
    final SharedPreferences prefs = await _prefs;
    final value_nickname = prefs.getString('key_nickname');
    final value_dart = prefs.getBool('key_dart');
    final value_js = prefs.getBool('key_js');
    final value_java = prefs.getBool('key_java');
    setState(() {
      controller.text = (value_nickname==null ? '' : value_nickname);
      this.value_dart = (value_dart==null ? false : value_dart);
      this.value_js = (value_js==null ? false : value_js);
      this.value_java = (value_java==null ? false : value_java);
    });
  }

  //将值存储到缓存
  void saveInfo(String value_nickname) async{
    final SharedPreferences prefs = await _prefs;
    prefs.setString('key_nickname', value_nickname);
    prefs.setBool('key_dart',  this.value_dart);
    prefs.setBool('key_js', this.value_js);
    prefs.setBool('key_java', this.value_java);
  }

  @override
  Widget build(BuildContext context) {
   return Scaffold(
     appBar: AppBar(
       title: Text('SharedPreferences示例'),
     ),
     body: Container(
       padding: EdgeInsets.all(10.0),
       child: Column(
         crossAxisAlignment: CrossAxisAlignment.center,
         children: [
           TextField(
             controller: controller,
             decoration: InputDecoration(
               labelText: '昵称',
               hintText: '请输入昵称'
             ),
           ),
           Text('你喜欢的编程语言'),
           Row(
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               Text('Dart'),
               Switch(
                 value: value_dart,
                 onChanged: (isChanged){
                   setState(() {
                     this.value_dart = isChanged;
                   });
               })
             ],
           ),
           Row(
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               Text('Javascript'),
               Switch(
                 value: value_js,
                 onChanged: (isChanged){
                   setState(() {
                     this.value_js = isChanged;
                   });
               })
             ],
           ),
           Row(
             mainAxisAlignment: MainAxisAlignment.center,
             children: [
               Text('Java'),
               Switch(
                 value: value_java,
                 onChanged: (isChanged){
                   setState(() {
                     this.value_java = isChanged;
                   });
               })
             ],
           ),
           MaterialButton(
             child: Text('保存'),
             onPressed: (){
               this.saveInfo(controller.text);
             },
           )
         ],
       ),
     ),
   );
  }
}



部分素材资源来源网站,本站提供免费下载,如有侵权请联系站长马上删除!
上一篇:Flutter笔记65:Fluro路由组件的简单使用 下一篇:Flutter笔记67:path_provider文件存储插件
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
选择头像:
最新评论

猜你喜欢