ios開發(fā)Flutter之?dāng)?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ù)可以使用事務(wù)處理
// 添加數(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()
關(guān)閉數(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之?dāng)?shù)據(jù)存儲的詳細內(nèi)容,更多關(guān)于ios Flutter數(shù)據(jù)存儲的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
iOS實現(xiàn)無限循環(huán)滾動的TableView實戰(zhàn)教程
這篇文章主要給大家介紹了關(guān)于iOS實現(xiàn)無限循環(huán)滾動的TableView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。2017-05-05iOS開發(fā)中Swift3 監(jiān)聽UITextView文字改變的方法(三種方法)
在項目中使用文本輸入框出UITextField之外還會經(jīng)常使用 UITextView ,難免會有需求監(jiān)聽UITextView文本框內(nèi)文本數(shù)量.下面介紹在swift3中兩種常用方式,需要的朋友參考下吧2016-11-11iOS應(yīng)用中發(fā)送HTTP的get請求以及HTTP異步請求的方法
這篇文章主要介紹了iOS應(yīng)用中發(fā)送HTTP的get請求以及HTTP異步請求的方法,代碼為傳統(tǒng)的Objective-C語言,說明都簡單地融入于注釋之中,需要的朋友可以參考下2016-02-02CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決
這篇文章主要介紹了CocoaPods 出現(xiàn)LoadError - cannot load such file -- nanaimo錯誤解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04