ios開發(fā)Flutter之數(shù)據(jù)存儲
偏好存儲
shared_preferences 類比iOS中的UserDefaults,使用方法比較簡單。 地址戳這里 pub get之后會自動出現(xiàn)一個這樣的文件generated_plugin_registrant.dart

數(shù)據(jù)存儲:
void _incrementCounter() {
//創(chuàng)建對象,用于操作存儲和讀取。
SharedPreferences.getInstance().then((SharedPreferences prefs) {
setState(() {
_counter++;
});
prefs.setInt('counter', _counter);
});
}
數(shù)據(jù)讀?。?/p>
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即可。感覺挺奇怪的一個錯誤 感謝大佬,問題解決鏈接
創(chuàng)建表
1.getDatabasesPath來到了Documents下的目錄 2.join(value, 'test_db.db')使用的是一個path的pub庫配合使用 3.openDatabase打開數(shù)據(jù)庫,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來使用。 執(zhí)行之后發(fā)現(xiàn)已經(jīng)創(chuàng)建成功了,大小8kb, 是一個空表。

數(shù)據(jù)插入
_db插入數(shù)據(jù)可以使用事務處理
// 添加數(shù)據(jù) 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));
});
數(shù)據(jù)查詢
// 數(shù)據(jù)查詢 SELECT 列名稱 FROM 表名稱 *通配符
_db.rawQuery('SELECT * FROM LK_Text').then((value) => print(value));

數(shù)據(jù)修改
// 修改數(shù)據(jù) UPDATE 表名稱 SET 列名稱 = 新值 WHERE 列名稱 = 某值
_db.rawUpdate('UPDATE LK_TEXT SET age = 18 WHERE age = 16');

刪除表
1._db.delete刪除表 2._db.close()關閉數(shù)據(jù)庫
_db
.rawQuery('SELECT * FROM LK_Text')
.then((value) => print(value))
.then((value) {
// 刪除表
_db.delete('LK_Text').then((value) => _db.close());
});
切記:由于這里是異步的操作,注意執(zhí)行的順序!! 校驗的話還是很簡單,再次寫入數(shù)據(jù)的時候會報錯。

刪除數(shù)據(jù)庫
// 刪除數(shù)據(jù)庫
getDatabasesPath()
.then((value) => join(value, 'test_db.db'))
.then((value) => deleteDatabase(value));
整體來說還是比較簡單的,主要是把sqlite語句寫正確。
以上就是ios開發(fā)Flutter之數(shù)據(jù)存儲的詳細內(nèi)容,更多關于ios Flutter數(shù)據(jù)存儲的資料請關注腳本之家其它相關文章!
相關文章
iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法
這篇文章主要介紹了iOS應用開發(fā)中使UITextField實現(xiàn)placeholder屬性的方法,示例代碼為傳統(tǒng)的Objective-C語言,需要的朋友可以參考下2016-04-04
IOS 開發(fā)中發(fā)送e-mail的幾種方法總結
這篇文章主要介紹了IOS 開發(fā)中發(fā)送e-mail的幾種方法總結的相關資料,需要的朋友可以參考下2017-03-03
iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫詳解
這篇文章主要給大家介紹了關于iOS開發(fā)中如何優(yōu)雅的調(diào)試數(shù)據(jù)庫的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2017-12-12

