偏好存储

shared_preferences 类比iOS中的UserDefaults,运用方法比较简单。 地址戳这儿 pub get之后会自动呈现一个这样的文件generated_plugin_registrant.dart

Flutter之数据存储
数据存储:

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, 是一个空表。

Flutter之数据存储

数据刺进

_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());
    });

切记:由于这儿是异步的操作,留意履行的顺序!! 校验的话还是很简单,再次写入数据的时候会报错。

Flutter之数据存储

删去数据库

    // 删去数据库
    getDatabasesPath()
        .then((value) => join(value, 'test_db.db'))
        .then((value) => deleteDatabase(value));

全体来说还是比较简单的,主要是把sqlite语句写正确。

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。