網站首頁 編程語言 正文
偏好存儲
shared_preferences
類比iOS中的UserDefaults
,使用方法比較簡單。 地址戳這里 pub get
之后會自動出現一個這樣的文件generated_plugin_registrant.dart
數據存儲:
void _incrementCounter() { //創建對象,用于操作存儲和讀取。 SharedPreferences.getInstance().then((SharedPreferences prefs) { setState(() { _counter++; }); prefs.setInt('counter', _counter); }); }
數據讀取:
SharedPreferences.getInstance().then((SharedPreferences prefs) { setState(() { _counter = prefs.getInt('counter') ?? 0; }); });
sqlite
使用sqlite需要搭配著path一起使用,在使用的過程中踩了一個坑, 明明我安裝了CocoaPods
卻一直提示我CocoaPods not installed
Warning: CocoaPods not installed. Skipping pod install.?
CocoaPods is used to retrieve the iOS and macOS platform side's plugin code?
that responds to your plugin usage on the Dart side.?
Without CocoaPods, plugins will not work on iOS or macOS.?
For more info, see https://flutter.dev/platform-plugins To install?
see https://guides.cocoapods.org/using/getting-started.html#installation for instructions.
最后解決辦法 1;打開終端 2; 輸入open /Applications/Android\ Studio.app
即可。感覺挺奇怪的一個錯誤 感謝大佬,問題解決鏈接
創建表
1.getDatabasesPath
來到了Documents
下的目錄 2.join(value, 'test_db.db')
使用的是一個path
的pub庫配合使用 3.openDatabase
打開數據庫,onCreate
建表 // 建表 CREATE TABLE 表名(,,)
late Database _db; @override void initState() { super.initState(); _initDatabase().then((value) => _db = value); } Future<Database> _initDatabase() async { Database db = await getDatabasesPath() .then((value) => join(value, 'test_db.db')) .then((value) => openDatabase(value, version: 1, onCreate: (Database db, int version) async { // 建表 await db.execute( 'CREATE TABLE LK_Text(id INTEGER PRIMARY KEY,name TEXT, age INT)'); })); return db; }
Future<String> getDatabasesPath() => databaseFactory.getDatabasesPath();
是一個Future
所以需要async
配合著await
來使用。 執行之后發現已經創建成功了,大小8kb, 是一個空表。
數據插入
_db插入數據可以使用事務處理
// 添加數據 INSERT INTO 表名 VALUES (值1,值2,...)
_db.transaction((txn) async { txn .rawInsert('INSERT INTO LK_Text(name,age) VALUES("zhangsan",16)') .then((value) => print(value)); txn .rawInsert('INSERT INTO LK_Text(name,age) VALUES("lisi",17)') .then((value) => print(value)); });
數據查詢
// 數據查詢 SELECT 列名稱 FROM 表名稱 *通配符
_db.rawQuery('SELECT * FROM LK_Text').then((value) => print(value));
數據修改
// 修改數據 UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
_db.rawUpdate('UPDATE LK_TEXT SET age = 18 WHERE age = 16');
刪除表
1._db.delete
刪除表 2._db.close()
關閉數據庫
_db .rawQuery('SELECT * FROM LK_Text') .then((value) => print(value)) .then((value) { // 刪除表 _db.delete('LK_Text').then((value) => _db.close()); });
切記:由于這里是異步的操作,注意執行的順序!! 校驗的話還是很簡單,再次寫入數據的時候會報錯。
刪除數據庫
// 刪除數據庫 getDatabasesPath() .then((value) => join(value, 'test_db.db')) .then((value) => deleteDatabase(value));
整體來說還是比較簡單的,主要是把sqlite
語句寫正確。
原文鏈接:https://juejin.cn/post/7096383768096669710
相關推薦
- 2022-10-09 玩轉Go命令行工具Cobra_Golang
- 2022-07-15 Python中else怎么用?else的用法總結_python
- 2022-08-11 python?scatter繪制散點圖_python
- 2022-04-15 基于Redis?zSet實現滑動窗口對短信進行防刷限流的問題_Redis
- 2021-12-11 C語言中數據在內存如何存儲_C 語言
- 2022-07-13 查看nginx連接數
- 2022-04-23 Docker?Compose快速部署多容器服務實戰的實例詳解_docker
- 2022-08-16 python可視化分析繪制散點圖和邊界氣泡圖_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支