日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

ios開發Flutter之數據存儲_IOS

作者:weak_PG ? 更新時間: 2022-09-13 編程語言

偏好存儲

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) =&gt; print(value))
        .then((value) {
      // 刪除表
      _db.delete('LK_Text').then((value) =&gt; _db.close());
    });

切記:由于這里是異步的操作,注意執行的順序!! 校驗的話還是很簡單,再次寫入數據的時候會報錯。

刪除數據庫

    // 刪除數據庫
    getDatabasesPath()
        .then((value) => join(value, 'test_db.db'))
        .then((value) => deleteDatabase(value));

整體來說還是比較簡單的,主要是把sqlite語句寫正確。

原文鏈接:https://juejin.cn/post/7096383768096669710

欄目分類
最近更新