zoukankan      html  css  js  c++  java
  • flutter shared_preferences相当于iOS的UserDefaults 安卓的SharedPreferences

    import 'dart:async';
    
    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    
    void main() {
      runApp(new MaterialApp(home: new MyApp()));
    }
    
    
    
    class MyApp extends StatelessWidget {
      final String mUserName = "userName";
      final _userNameController = new TextEditingController();
    
      @override
      Widget build(BuildContext context) {
        save() async{
          SharedPreferences prefs = await SharedPreferences.getInstance();
          prefs.setString(mUserName, _userNameController.value.text.toString());
        }
    
        Future<String> get() async {
          var userName;
    
          SharedPreferences prefs = await SharedPreferences.getInstance();
          userName = prefs.getString(mUserName);
          return userName;
        }
    
        return new Builder(builder: (BuildContext context) {
          return new Scaffold(
            appBar:  AppBar(
              title:  Text("SharedPreferences"),
            ),
            body:  Center(
              child: new Builder(builder: (BuildContext context){
                return
                  Column(
                    children: <Widget>[
                      TextField(
                        controller: _userNameController,
                        decoration:  InputDecoration(
                            contentPadding: const EdgeInsets.only(top: 10.0),
                            icon:  Icon(Icons.perm_identity),
                            labelText: "请输入用户名",
                            helperText: "注册时填写的名字"),
                      ),
                      RaisedButton(
                          color: Colors.blueAccent,
                          child: Text("存储"),
                          onPressed: () {
                            save();
                            Scaffold.of(context).showSnackBar(
                                new SnackBar(content:  Text("数据存储成功")));
                          }),
                      RaisedButton(
                          color: Colors.greenAccent,
                          child: Text("获取"),
                          onPressed: () {
                            Future<String> userName = get();
                            userName.then((String userName) {
                              Scaffold.of(context).showSnackBar(
                                  SnackBar(content: Text("数据获取成功:$userName")));
                            });
                          }),
                    ],
                  );
              }),
            ),
          );
        });
      }
    }

  • 相关阅读:
    NodeJS 难点(网络,文件)的 核心 stream 二:stream是什么
    NodeJS 难点(网络,文件)的 核心 stream 一:Buffer
    了解了这些才能开始发挥jQuery的威力
    一般公司的大体要求
    js 的垃圾回收器 原理 坑 优化-- 待续
    iframe 问题集合
    图片预加载 js css预加载
    各种插件
    Django REST framework快速入门指南
    Vue.js devtool插件安装后无法使用的解决办法
  • 原文地址:https://www.cnblogs.com/gaozhang12345/p/12089955.html
Copyright © 2011-2022 走看看