当前位置:网站首页>【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
【Flutter】shared_ Preferences local storage (introduction | install the shared_preferences plug-in | use the shared_preferences process)
2022-07-03 02:28:00 【Programmer community】
List of articles
- One 、shared_preferences Introduction to local storage plug-ins
- Two 、 install shared_preferences plug-in unit
- 3、 ... and 、 Use shared_preferences technological process
- Four 、 Complete code example
- 5、 ... and 、 Related resources
One 、shared_preferences Introduction to local storage plug-ins
shared_preferences yes Flutter Provided Local data access plug-in unit ;
On different platforms , Develop based on different mechanisms , Such as Android Based on SharedPreferences Development , iOS Based on NSUserDefaults Development ;
Accessing local files is a time-consuming operation , Therefore access shared_preferences Storage is a Asynchronous operations ;
Two 、 install shared_preferences plug-in unit
install shared_preferences plug-in unit :
shared_preferences Plug-in address : https://pub.dev/packages/shared_preferences
according to https://pub.dev/packages/shared_preferences/install Address the installation tutorial to install ;
First , stay pubspec.yaml In profile , Import plug-in package :
dependencies: shared_preferences: ^2.0.7then , Download the plug-in package , Click on Pub Get Button , Download the plug-in package into this project ;

Last , stay Dart Import the following code into the code , You can use shared_preferences ;
import 'package:shared_preferences/shared_preferences.dart';3、 ... and 、 Use shared_preferences technological process
After completing the above installation shared_preferences After the plug-in , To start using ;
First , Import plug-in package ;
import 'package:shared_preferences/shared_preferences.dart';then , obtain shared_preferences example ;
final prefs = await SharedPreferences.getInstance();Last , Through the above shared_preferences Instances can Set up / Read Stored key value pair value ;
/// Set the value prefs.setInt('counter', counter);/// Get value final counter = prefs.getInt('counter') ?? 0;}If you want to delete data , call shared_preferences Example of remove Method ;
prefs.remove('counter');The picture below is Flutter Of SharedPreferences Class , Focus on data access methods ;

Data storage example :
_setValue() async {
/// First get SharedPreferences example SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() {
textInfo = ' Save string " Xiao Wang " To shared_preferences complete '; }); /// Save data to SharedPreferences in await prefs.setString("name", " Xiao Wang "); }Data access examples :
_getValue() async {
/// First get SharedPreferences example SharedPreferences prefs = await SharedPreferences.getInstance(); /// from SharedPreferences get data String? name = await prefs.getString("name"); setState(() {
textInfo = ' from shared_preferences Take out the data " ${name} "'; }); }Four 、 Complete code example
import 'package:flutter/material.dart';import 'package:shared_preferences/shared_preferences.dart';void main() {
runApp(MyApp());}class MyApp extends StatefulWidget {
const MyApp({
Key? key}) : super(key: key); @override _MyAppState createState() => _MyAppState();}class _MyAppState extends State<MyApp> {
String textInfo = " Click the button to save the data to shared_preferences in "; _setValue() async {
/// First get SharedPreferences example SharedPreferences prefs = await SharedPreferences.getInstance(); setState(() {
textInfo = ' Save string " Xiao Wang " To shared_preferences complete '; }); /// Save data to SharedPreferences in await prefs.setString("name", " Xiao Wang "); } _getValue() async {
/// First get SharedPreferences example SharedPreferences prefs = await SharedPreferences.getInstance(); /// from SharedPreferences get data String? name = await prefs.getString("name"); setState(() {
textInfo = ' from shared_preferences Take out the data " ${name} "'; }); } @override Widget build(BuildContext context) {
return MaterialApp( home: Scaffold( appBar: AppBar( title: Text("shared_preferences The data access "), ), body: Column( children: [ Text(textInfo), ElevatedButton( onPressed: (){
_setValue(); }, child: Text(" Store data to shared_preferences in "), ), ElevatedButton( onPressed: (){
_getValue(); }, child: Text(" from shared_preferences Get data in "), ), ], ), ), ); }}Execution results :

5、 ... and 、 Related resources
Reference material :
- Flutter Official website : https://flutter.dev/
- Flutter Plug in download address : https://pub.dev/packages
- Flutter Developing documents : https://flutter.cn/docs ( Strongly recommend )
- official GitHub Address : https://github.com/flutter
- Flutter The Chinese community : https://flutter.cn/
- Flutter Practical tutorial : https://flutter.cn/docs/cookbook
- Flutter CodeLab : https://codelabs.flutter-io.cn/
- Dart Chinese document : https://dart.cn/
- Dart Developer website : https://api.dart.dev/
- Flutter Chinese net : https://flutterchina.club/ , http://flutter.axuer.com/docs/
- Flutter Related issues : https://flutterchina.club/faq/ ( It is recommended to watch it at the introductory stage )
- GitHub Upper Flutter Open source examples : https://download.csdn.net/download/han1202012/15989510
- Flutter Practical e-books : https://book.flutterchina.club/chapter1/
- Dart Language practice website : https://dartpad.dartlang.org/
Important topics :
- Flutter Animation reference documentation : https://flutterchina.club/animations/
Blog source download :
GitHub Address : https://github.com/han1202012/flutter_shared_preferences ( Keep updating with the progress of the blog , There may not be the source code of this blog )
Blog source snapshot : ( The source code snapshot of this blog , You can find the source code of this blog )
边栏推荐
- Startup mode and scope builder of collaboration in kotlin
- Gbase 8C system table PG_ collation
- Cancellation of collaboration in kotlin, side effects of cancellation and overtime tasks
- RestCloud ETL 跨库数据聚合运算
- elastic stack
- [Yu Yue education] China Ocean University job search OMG reference
- The Sandbox阐释对元宇宙平台的愿景
- [Yu Yue education] reference materials of love psychology of China University of mining and technology
- awk从入门到入土(2)认识awk内置变量和变量的使用
- 人脸识别6- face_recognition_py-基于OpenCV使用Haar级联与dlib库进行人脸检测及实时跟踪
猜你喜欢

PyTorch 卷积网络正则化 DropBlock

返回一个树形结构数据

Job object of collaboration in kotlin

【翻译】具有集中控制平面的现代应用负载平衡
![[Hcia]No.15 Vlan间通信](/img/59/a467c5920cbccb72040f39f719d701.jpg)
[Hcia]No.15 Vlan间通信
![[translation] the background project has joined the CNCF incubator](/img/0b/e3d2674b1a1cba3ea398cbcb1a018a.png)
[translation] the background project has joined the CNCF incubator
[advanced ROS] Lesson 6 recording and playback in ROS (rosbag)

Detailed introduction to the usage of Nacos configuration center

Producer consumer model based on thread pool (including blocking queue)

Awk from introduction to earth (0) overview of awk
随机推荐
PyTorch 卷积网络正则化 DropBlock
oauth2.0鉴权,登录访问 “/oauth/token”,请求头Authorization(basicToken)如何取值???
Gbase 8C system table PG_ collation
[translation] modern application load balancing with centralized control plane
Leetcode (540) -- a single element in an ordered array
【 tutoriel】 Chrome ferme les cors et les messages de la politique inter - domaines et apporte des cookies à travers les domaines
GBase 8c系统表-pg_authid
【ROS进阶篇】第六讲 ROS中的录制与回放(rosbag)
Qt之QComboBox添加QCheckBox(下拉列表框插入复选框,含源码+注释)
8 free, HD, copyright free video material download websites are recommended
Unrecognized SSL message, plaintext connection?
Face recognition 6-face_ recognition_ Py based on OpenCV, face detection and real-time tracking using Haar cascade and Dlib Library
require.context
Gbase 8C system table PG_ amop
【CodeForces】CF1338A - Powered Addition【二进制】
为什么会选择框架?选择什么样的框架
Thread safe singleton mode
The data in servlet is transferred to JSP page, and the problem cannot be displayed using El expression ${}
Gbase 8C system table PG_ conversion
The Sandbox阐释对元宇宙平台的愿景