1、在pubspec.yaml中安装
shared_preferences: ^0.5.12+22、读取方法如:
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); }, ) ], ), ), ); } }