欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

ios開發(fā)Flutter之?dāng)?shù)據(jù)存儲(chǔ)

 更新時(shí)間:2022年07月20日 15:42:53   作者:weak_PG  
這篇文章主要為大家介紹了ios開發(fā)Flutter之?dāng)?shù)據(jù)存儲(chǔ)的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

偏好存儲(chǔ)

shared_preferences 類比iOS中的UserDefaults,使用方法比較簡(jiǎn)單。 地址戳這里 pub get之后會(huì)自動(dòng)出現(xiàn)一個(gè)這樣的文件generated_plugin_registrant.dart

數(shù)據(jù)存儲(chǔ):

void _incrementCounter() {
  //創(chuàng)建對(duì)象,用于操作存儲(chǔ)和讀取。
  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一起使用,在使用的過(guò)程中踩了一個(gè)坑, 明明我安裝了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即可。感覺(jué)挺奇怪的一個(gè)錯(cuò)誤 感謝大佬,問(wèn)題解決鏈接

創(chuàng)建表

1.getDatabasesPath來(lái)到了Documents下的目錄 2.join(value, 'test_db.db')使用的是一個(gè)path的pub庫(kù)配合使用 3.openDatabase打開數(shù)據(jù)庫(kù),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();是一個(gè)Future所以需要async配合著await來(lái)使用。 執(zhí)行之后發(fā)現(xiàn)已經(jīng)創(chuàng)建成功了,大小8kb, 是一個(gè)空表。

數(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ù)庫(kù)

  _db
        .rawQuery('SELECT * FROM LK_Text')
        .then((value) =&gt; print(value))
        .then((value) {
      // 刪除表
      _db.delete('LK_Text').then((value) =&gt; _db.close());
    });

切記:由于這里是異步的操作,注意執(zhí)行的順序??! 校驗(yàn)的話還是很簡(jiǎn)單,再次寫入數(shù)據(jù)的時(shí)候會(huì)報(bào)錯(cuò)。

刪除數(shù)據(jù)庫(kù)

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

整體來(lái)說(shuō)還是比較簡(jiǎn)單的,主要是把sqlite語(yǔ)句寫正確。

以上就是ios開發(fā)Flutter之?dāng)?shù)據(jù)存儲(chǔ)的詳細(xì)內(nèi)容,更多關(guān)于ios Flutter數(shù)據(jù)存儲(chǔ)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論