Flutter 之 数据存储

path_provider 文件

//创建文件目录函数

/*

* /// 获取文档目录文件

Future _getLocalDocumentFile() async {

final dir = await getApplicationDocumentsDirectory();

return File('${dir.path}/str.txt');

}

/// 获取临时目录文件

Future _getLocalTemporaryFile() async {

final dir = await getTemporaryDirectory();

return File('${dir.path}/str.txt');

}

/// 获取应用程序目录文件

Future _getLocalSupportFile() async {

final dir = await getApplicationSupportDirectory();

return File('${dir.path}/str.txt');

}

*

* */

  Future_getLocalFile()async {

print("创建文件");

// 获取应用目录

    String dir = (await getApplicationDocumentsDirectory()).path;

return File('$dir/content.txt');

}

//写文件函数

  Future_writeFileContent(String content)async {

return  (await _getLocalFile()).writeAsString(content);

}

SharedPreferences 键值对信息

// 读取 SharedPreferences 中 key 为 counter 的值

  Future_readSPCounter()async {

SharedPreferences prefs =await SharedPreferences.getInstance();

int  counter = (prefs.getInt('counter') ??0);

return counter;

}

// 递增写入 SharedPreferences 中 key 为 counter 的值

  Future_writeSPCounter()async {

SharedPreferences prefs =await SharedPreferences.getInstance();

int counter = (prefs.getInt('counter') ??0) +1;

prefs.setInt('counter',counter);

}

sqflite 数据库


dependencies:

    flutter:

        sdk:flutter

    sqflite:

1.定义狗数据模型 ---- 2.打开数据库 ---- 3.创建表 ---- 4.插入数据 ---- 5.检索表 ---- 6.更新数据库 ---- 7.删除数据 

void PeopleDB()async {

WidgetsFlutterBinding.ensureInitialized();

//打开数据库

  final Futuredatabase = openDatabase(

//创建s表

    join(await getDatabasesPath(),'peoplegie_database.db'),

onCreate: (db, version) {

return db.execute(

"CREATE TABLE peoples(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",

);

},

version:1,

);

//插入数据

  FutureinsertPeople(People peopl)async {

final Database db =await database;

await db.insert(

'peoples',

peopl.toMap(),

conflictAlgorithm:ConflictAlgorithm.replace,

);

}

//检索列表

  Future>peoples()async {

final Database db =await database;

final List>maps =await db.query('peoples');

return List.generate(maps.length, (i) {

return People(

id:maps[i]['id'],

name:maps[i]['name'],

age:maps[i]['age'],

);

});

}

//更新数据

  FutureupdatePeople(People people)async {

final db =await database;

await db.update(

'peoples',

people.toMap(),

where:"id = ?",

whereArgs: [people.id],

);

}

//删除数据

  FuturedeletePeople(int id)async {

final db =await database;

await db.delete(

'Peoples',

where:"id = ?",

whereArgs: [id],

);

}

var fido =People(

id:0,

name:'Fido',

age:35,

);

await insertPeople(fido);

print(await peoples());

fido =People(

id:fido.id,

name:fido.name,

age:fido.age +7,

);

await updatePeople(fido);

print(await peoples());

await deletePeople(fido.id);

print(await peoples());

}

class People {

final int id;

final String name;

final int age;

People({required this.id,required this.name,required this.age});

MaptoMap() {

return {

'id':id,

'name':name,

'age':age,

};

}

@override

  String toString() {

return 'People{id: $id, name: $name, age: $age}';

}

}

你可能感兴趣的:(Flutter 之 数据存储)