SharePreference

知识点:
当BuildContext在Scaffold之前时,调用Scaffold.of(context)会报错。这时可以通过Builder Widget来解决:

异步操作:
save() async{
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setString(mUserName, _userNameController.value.text.toString());//第一个参数是key,第二个参数是value;
}

Future<String> get() async {
var userName;
SharedPreferences prefs = await SharedPreferences.getInstance();
userName = prefs.getString(mUserName);
return userName;
}
Future<String> userName = get();
userName.then((String userName) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("数据获取成功:$userName")));
});
 



import 'dart:async';


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

void main() {
runApp(MaterialApp(home: MyApp()));
}



class MyApp extends StatelessWidget {
final String mUserName = "userName";
final _userNameController = 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 Builder(builder: (BuildContext context) {
return 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(
child: Text("存储"),
onPressed: () {
save();
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("数据存储成功")));
}),
RaisedButton(
child: Text("获取"),
onPressed: () {
Future<String> userName = get();
userName.then((String userName) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("数据获取成功:$userName")));
});
}),
],
);
}),
),
);
});
}
}




下面这个示例是我根据上面的例子自己写的:
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
TextEditingController _textEditingController=TextEditingController();
saveData() async {
SharedPreferences pref = await SharedPreferences.getInstance();
pref.setString('keyvalue', _textEditingController.text);
}

Future<String> getData()async{
SharedPreferences pref=await SharedPreferences.getInstance();
var backData;
backData=pref.getString('keyvalue');
return backData;
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(''),
),
body: Builder(builder: (context) {
return Center(
child: Column(
children: <Widget>[
TextField(
controller: _textEditingController,
decoration: InputDecoration(
hintText: '请输入数据:',
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(28)),
),
),
RaisedButton(
child: Text('保存'),
onPressed: () {
saveData();
Scaffold.of(context).showSnackBar(SnackBar(content: Text('保存数据${_textEditingController.text}',),),);
}),
RaisedButton(
color: Colors.greenAccent,
child: Text("获取"),
onPressed: () {
Future<String> userName = getData();
userName.then((String value) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text("数据获取成功:$value")));
});
}),
],
),
);
}),
);
}
}
 
posted @ 2019-05-15 11:25  braveheart007  阅读(211)  评论(0编辑  收藏  举报