日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【Flutter】shared_preferences 本地存储 ( 简介 | 安装 shared_preferences 插件 | 使用 shared_preferences 流程 )

發布時間:2025/6/17 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【Flutter】shared_preferences 本地存储 ( 简介 | 安装 shared_preferences 插件 | 使用 shared_preferences 流程 ) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

文章目錄

  • 一、shared_preferences 本地存儲插件簡介
  • 二、安裝 shared_preferences 插件
  • 三、使用 shared_preferences 流程
  • 四、完整代碼示例
  • 五、相關資源





一、shared_preferences 本地存儲插件簡介



shared_preferences 是 Flutter 提供的 本地數據存取 插件 ;

在不同的平臺 , 基于不同的機制開發 , 如 Android 平臺中基于 SharedPreferences 開發 , iOS 平臺中基于 NSUserDefaults 開發 ;

訪問本地文件是耗時操作 , 因此訪問 shared_preferences 存儲是一個 異步操作 ;





二、安裝 shared_preferences 插件



安裝 shared_preferences 插件 :

shared_preferences 插件地址 : https://pub.dev/packages/shared_preferences

按照 https://pub.dev/packages/shared_preferences/install 地址的安裝教程進行安裝 ;

首先 , 在 pubspec.yaml 配置文件中 , 導入插件包 :

dependencies:shared_preferences: ^2.0.7

然后 , 下載插件包 , 點擊 Pub Get 按鈕 , 下載插件包到本工程中 ;

最后 , 在 Dart 代碼中導入如下代碼 , 即可使用 shared_preferences ;

import 'package:shared_preferences/shared_preferences.dart';



三、使用 shared_preferences 流程



在完成了上述安裝 shared_preferences 插件之后 , 才能開始使用 ;

首先 , 導入插件包 ;

import 'package:shared_preferences/shared_preferences.dart';

然后 , 獲取 shared_preferences 實例 ;

final prefs = await SharedPreferences.getInstance();

最后 , 通過上述 shared_preferences 實例可以 設置 / 讀取 存儲的鍵值對數值 ;

/// 設置值 prefs.setInt('counter', counter);/// 獲取值 final counter = prefs.getInt('counter') ?? 0;}

如果要刪除數據 , 調用 shared_preferences 實例的 remove 方法 ;

prefs.remove('counter');

下圖是 Flutter 的 SharedPreferences 類提供的所有方法 , 重點關注數據的訪問方法 ;

數據存儲示例 :

_setValue() async {/// 先獲取 SharedPreferences 實例SharedPreferences prefs = await SharedPreferences.getInstance();setState(() {textInfo = '保存字符串 " 小王 " 到 shared_preferences 完成';});/// 將數據保存到 SharedPreferences 中await prefs.setString("name", "小王");}

數據訪問示例 :

_getValue() async {/// 先獲取 SharedPreferences 實例SharedPreferences prefs = await SharedPreferences.getInstance();/// 從 SharedPreferences 獲取數據String? name = await prefs.getString("name");setState(() {textInfo = '從 shared_preferences 取出數據 " ${name} "';});}



四、完整代碼示例



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 = "點擊按鈕保存數據到 shared_preferences 中";_setValue() async {/// 先獲取 SharedPreferences 實例SharedPreferences prefs = await SharedPreferences.getInstance();setState(() {textInfo = '保存字符串 " 小王 " 到 shared_preferences 完成';});/// 將數據保存到 SharedPreferences 中await prefs.setString("name", "小王");}_getValue() async {/// 先獲取 SharedPreferences 實例SharedPreferences prefs = await SharedPreferences.getInstance();/// 從 SharedPreferences 獲取數據String? name = await prefs.getString("name");setState(() {textInfo = '從 shared_preferences 取出數據 " ${name} "';});}@overrideWidget build(BuildContext context) {return MaterialApp(home: Scaffold(appBar: AppBar(title: Text("shared_preferences 數據訪問"),),body: Column(children: [Text(textInfo),ElevatedButton(onPressed: (){_setValue();},child: Text("存儲數據到 shared_preferences 中"),),ElevatedButton(onPressed: (){_getValue();},child: Text("從 shared_preferences 中獲取數據"),),],),),);} }

執行結果 :





五、相關資源



參考資料 :

  • Flutter 官網 : https://flutter.dev/
  • Flutter 插件下載地址 : https://pub.dev/packages
  • Flutter 開發文檔 : https://flutter.cn/docs ( 強烈推薦 )
  • 官方 GitHub 地址 : https://github.com/flutter
  • Flutter 中文社區 : https://flutter.cn/
  • Flutter 實用教程 : https://flutter.cn/docs/cookbook
  • Flutter CodeLab : https://codelabs.flutter-io.cn/
  • Dart 中文文檔 : https://dart.cn/
  • Dart 開發者官網 : https://api.dart.dev/
  • Flutter 中文網 : https://flutterchina.club/ , http://flutter.axuer.com/docs/
  • Flutter 相關問題 : https://flutterchina.club/faq/ ( 入門階段推薦看一遍 )
  • GitHub 上的 Flutter 開源示例 : https://download.csdn.net/download/han1202012/15989510
  • Flutter 實戰電子書 : https://book.flutterchina.club/chapter1/
  • Dart 語言練習網站 : https://dartpad.dartlang.org/

重要的專題 :

  • Flutter 動畫參考文檔 : https://flutterchina.club/animations/

博客源碼下載 :

  • GitHub 地址 : https://github.com/han1202012/flutter_shared_preferences ( 隨博客進度一直更新 , 有可能沒有本博客的源碼 )

  • 博客源碼快照 : ( 本篇博客的源碼快照 , 可以找到本博客的源碼 )

總結

以上是生活随笔為你收集整理的【Flutter】shared_preferences 本地存储 ( 简介 | 安装 shared_preferences 插件 | 使用 shared_preferences 流程 )的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。