Today to introduce you is the local storage method, it and The Android SharedPreferences in the same effect, the use is very simple, and finally I will package it into a tool class, for everyone to use, the next look at my simple introduction ~
- What is a shared_preferences
- Rely on
- Guide package
- Simple to use
- Shared_preferences tools
What is a shared_preferences
SharedPreferences SharedPreferences is a storage tool that is commonly used to store local data in the form of key and value pairs. Since it is also a time-consuming operation, it must be identified with async,await
Rely on
Dependencies: shared_preferences: ^ 0.5.12 + 4
Guide package
The import ‘package: shared_preferences/shared_preferences dart “;
Simple to use
void setString(String key, String value) async {
/** * Get the SharedPreferences instance */
SharedPreferences instance = await SharedPreferences.getInstance();
instance.setString(key, value);
}
Future<String> getString(String key) async {
/** * Get the SharedPreferences instance */
SharedPreferences instance = await SharedPreferences.getInstance();
return instance.getString(key);
}
Copy the code
Okay, so there’s nothing to say about this code, except that it’s set,get, okay
Shared_preferences also provides several methods for assigning different values:
Use the same as setString!
Shared_preferences provides a set of values, and there must be a way to get the values. Here are a few ways to get the values
As with getString(), you just pass in the key value
Shared_preferences also provides the remove method
/** * Delete the value */ corresponding to key
remove(String key) async{
SharedPreferences instance= await SharedPreferences.getInstance();
instance.remove(key);
}
Copy the code
Take a look at the full code:
import 'package:flutter/material.dart';
import 'package:flutter_trip/util/sp_util.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SPTest extends StatefulWidget {
@override
_SPTestState createState(a) => _SPTestState();
}
class _SPTestState extends State<SPTest> {
String TEST_KEY = "test_key";
String _mObtainData = "";
final TextEditingController _textcontroller = TextEditingController();
@override
Widget build(BuildContext context) {
getString(TEST_KEY).then((value) {
_mObtainData = value;
print("${value}");
});
return Column(
children: [
TextField(
controller: _textcontroller ,
decoration: InputDecoration(
hintText: "Please enter a stored value",
suffixIcon: IconButton(
icon: Icon(Icons.delete),
onPressed: (){
/** * refresh Build and reassign */ to the following Text
setState(() {
// delete the saved text in sp
remove(TEST_KEY);
// Delete the text on the keyboard
_textcontroller.clear();
});
},
)
),
onChanged: (value) {
/** * refresh Build and reassign */ to the following Text
setState(() {
setString(TEST_KEY, value);
});
},
),
Text("Stored data :\n${_mObtainData}",textAlign: TextAlign.center,style: TextStyle(fontSize: 30),),,); }void setString(String key, String value) async {
/** * Get the SharedPreferences instance */
SharedPreferences instance = await SharedPreferences.getInstance();
instance.setString(key, value);
}
Future<String> getString(String key) async {
/** * Get the SharedPreferences instance */
SharedPreferences instance = await SharedPreferences.getInstance();
return instance.get(key);
}
/** * Delete the value */ corresponding to Key
static remove(String key) async{ SharedPreferences instance= await SharedPreferences.getInstance(); instance.remove(key); }}Copy the code
Effect:
Take a look at log:
Comments written very detailed, not verbose, next it Sp package, to provide everyone to use
Shared_preferences tools
import 'package:shared_preferences/shared_preferences.dart'; / / rely on address: https://pub.dev/packages/shared_preferences / * * * SZJ 2020/11/20 * / class SpUtil {/ / / by / / / stored value in the form of generics -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / / -- -- -- -- -- -- -- -- -- -- he has to write a generic!!!!!!!!!! ---------- /// --------------------------------- static setData<T>(String key, T value) async { SharedPreferences instance = await SharedPreferences.getInstance(); switch (T) { case String: print("SpUtilsetData:T:${T}\t-- key:${key}"); instance.setString(key, value as String); break; case int: instance.setInt(key, value as int); break; case double: instance.setDouble(key, value as double); break; case bool: instance.setBool(key, value as bool); break; }} / / / for stored value / / / -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - / / / -- -- -- -- -- -- -- -- -- -- he has to write a generic!!!!!!!!!! ---------- /// --------------------------------- static Future<T> getDate<T>(String key) async{ SharedPreferences instance = await SharedPreferences.getInstance(); var t; switch (T) { case String: print("SpUtilgetData:T:${T}\t-- key:${key}"); t = await instance.getString(key); break; case int: t = instance.getInt(key); break; case double: t = instance.getDouble(key); break; case bool: t = instance.getBool(key); break; } return t; */ static setStringList(String key,List<String> value)async{SharedPreferences instance= await SharedPreferences.getInstance(); instance.setStringList(key, value); } static Future<List<String>> getStringList(String key) async{SharedPreferences instance= await SharedPreferences.getInstance(); return instance.getStringList(key); } / * * * * / delete key corresponding value static remove (String key) async {SharedPreferences instance = await SharedPreferences. GetInstance (); instance.remove(key); }}Copy the code
Use:
Remaining value:
SpUtil.setData<String>(TEST_KEY, value);
Copy the code
Values:
SpUtil.getDate<String>(TEST_KEY).then((value) { _mObtainData = value; Print (" ${value}"); });Copy the code
Util class is mainly convenient to use, some code is fixed, but no one can write every day, do not write easy to forget, and write tool class also improve the code reuse rate
Finally, there is an on/off button to tell whether to use the SpUtil utility class:
Here we go directly to the renderings :(shown at the bottom of the full code)
SPUtil tools
Chapter 1 :Flutter Future and FutureBuilder Asynchronous Operation (3.3)
Original is not easy, your thumbs up is my biggest support, leave your thumbs up ~