Flutter 本地存储/文件存储/数据库存储

非常不错的三种存储,能够持久化存储和读取,指定路径后可以实现即使App卸载了一样数据还存储在用户的设备内,下次下载依然读取原数据,使用起来非常方便。


Flutter支持Preferences(Shared Preferences and NSUserDefaults) 、文件、和Sqlite3。若想使用这个功能需要引入官方仓库的相应插件,下面详细介绍这三种存储方式的使用方法。

Preferences

等同iOS的NSUserDefaults和Android的SharedPreferences。

导入插件
  1. 打开项目的pubspec.yaml配置,在dependencies节点下新增配置:
    shared_preferences: ^0.4.1
  2. 点击开发工具提示的packages get按钮或者在命令行输入flutter packages get来同步第三方插件
  3. 在Dart文件中引入插件
    import 'package:shared_preferences/shared_preferences.dart';
使用示例
setUserName() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    prefs.setString(mUserName,"小明");
}

getUserName() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    userName = prefs.getString(mUserName);
}

displayUserName() {
    Future<String> userName = getUserName();
    userName.then((String userName) {
           Scaffold.of(context).showSnackBar(
                    SnackBar(content: Text("数据获取成功:$userName")));
    });
}

文件存储

导入插件
  1. 打开项目的pubspec.yaml配置,在dependencies节点下新增配置:
    path_provider: ^0.4.0
  2. 点击开发工具提示的packages get按钮或者在命令行输入flutter packages get来同步第三方插件
  3. 在Dart文件中引入插件
    import 'package:path_provider/path_provider.dart';
使用示例

获取文件路径方法:

  1. 获取应用缓存目录: getTemporaryDirectory
    • 类似iOS的NSTemporaryDirectory和Android的getCacheDir
  2. 获取应用文件目录: getApplicationDocumentsDirectory
    • 类似iOS的NSDocumentDirectory和Android上的AppData目录
    • 应用程序被删除时,系统会清除目录
  3. 存储卡: getExternalStorageDirectory
    • 仅支持Android平台
// 找到正确的本地路径
Future<String> get _localPath async {
  final directory = await getApplicationDocumentsDirectory();
  return directory.path;
}

// 创建对文件位置的引用
Future<File> get _localFile async {
  final path = await _localPath;
  return new File('$path/counter.txt');
}

// 将数据写入文件
Future<File> writeCounter(int counter) async {
  final file = await _localFile;
  // Write the file
  return file.writeAsString('$counter');
}

// 从文件中读取数据
Future<int> readCounter() async {
  try {
    final file = await _localFile;
    // Read the file
    String contents = await file.readAsString();

    return int.parse(contents);
  } catch (e) {
    // If we encounter an error, return 0
    return 0;
  }
}

Sqfite

SQLite plugin for Flutter. Get the default databases location. On Android, it is typically data/data/<package_name>/databases, On iOS, it is the Documents directory.

导入插件
  1. 打开项目的pubspec.yaml配置,在dependencies节点下新增配置:
    sqflite: ^1.0.0
  2. 点击开发工具提示的packages get按钮或者在命令行输入flutter packages get来同步第三方插件
  3. 在Dart文件中引入插件
    import 'package:sqflite/sqflite.dart';

使用示例

// 获取数据库文件的存储路径
var databasesPath = await getDatabasesPath();
String path = join(databasesPath, 'demo.db');

// 创建数据库表
db = await openDatabase(path, version: 1,
       onCreate: (Database db, int version) async {
   await db.execute('''
        CREATE TABLE $tableBook (
            $columnId INTEGER PRIMARY KEY, 
            $columnName TEXT, 
            $columnAuthor TEXT, 
            $columnPrice REAL, 
            $columnPublishingHouse TEXT)
          ''');
    });

// 插入数据
Future<int> rawInsert(String sql, [List<dynamic> arguments]);

Future<int> insert(String table, Map<String, dynamic> values,
      {String nullColumnHack, ConflictAlgorithm conflictAlgorithm});


// 查询数据
Future<List<Map<String, dynamic>>> rawQuery(String sql,
      [List<dynamic> arguments]);

Future<List<Map<String, dynamic>>> query(String table,
      {bool distinct,
      List<String> columns,
      String where,
      List<dynamic> whereArgs,
      String groupBy,
      String having,
      String orderBy,
      int limit,
      int offset});

// 更新数据
Future<int> rawUpdate(String sql, [List<dynamic> arguments]);

Future<int> update(String table, Map<String, dynamic> values,
      {String where,
      List<dynamic> whereArgs,
      ConflictAlgorithm conflictAlgorithm});

// 删除
Future<int> rawDelete(String sql, [List<dynamic> arguments]);

Future<int> delete(String table, {String where, List<dynamic> whereArgs});

// 关闭数据库
Future close() async => db.close();

参考学习相关文章:

Flutter 构建完整应用手册-持久化

Flutter数据存储之shared_preferences

Flutter中的本地存储

本博客所有文章如无特别注明均为原创。作者:flutter教程网复制或转载请以超链接形式注明转自 Flutter教程网
原文地址《Flutter 本地存储/文件存储/数据库存储
分享到:更多

相关推荐



Flutter教程网 官方QQ群:874592746

扫描下面二维码 加入Flutter教程网微信群:


关注公众号“Flutter前线”,各种Flutter项目实战经验技巧,干活知识,Flutter面试题答案,等你来领取。


发表评论

路人甲 表情
Ctrl+Enter快速提交

网友评论(2969)

-盐嗣逊团暇https://www.imdb.com/list/ls504481616/?mode=desktop&ref_=c_pl_gpt
-捕笆莆卤越https://www.imdb.com/list/ls504481654/?mode=desktop&ref_=c_pl_gpt
-占纠迸腥涝https://www.imdb.com/list/ls504481608/?mode=desktop&ref_=c_pl_gpt
-媒勇咨舜痰https://www.imdb.com/list/ls504481604/?mode=desktop&ref_=c_pl_gpt
-云陡呢涸悦https://www.imdb.com/list/ls504481601/?mode=desktop&ref_=c_pl_gpt
17717171333 6天前 回复
-芳侣谮档繁https://www.imdb.com/list/ls504482710/?mode=desktop&ref_=c_pl_gpt
-屎谮浇乙篮https://www.imdb.com/list/ls504482774/?mode=desktop&ref_=c_pl_gpt
-蓖儆繁靖哺https://www.imdb.com/list/ls504482754/?mode=desktop&ref_=c_pl_gpt
-宜捍亮适敝https://www.imdb.com/list/ls504482755/?mode=desktop&ref_=c_pl_gpt
-堵都靠创评https://www.imdb.com/list/ls504482709/?mode=desktop&ref_=c_pl_gpt
17748420826 6天前 回复
-偃偻罢菜惭https://www.imdb.com/list/ls504488078/?mode=desktop&ref_=c_pl_gpt
-酵枷谌心从https://www.imdb.com/list/ls504488056/?mode=desktop&ref_=c_pl_gpt
-祷橇蕾秆渭https://www.imdb.com/list/ls504488008/?mode=desktop&ref_=c_pl_gpt
-唐砍奖及从https://www.imdb.com/list/ls504489899/?mode=desktop&ref_=c_pl_gpt
-厝坪秆帽敝https://www.imdb.com/list/ls504489841/?mode=desktop&ref_=c_pl_gpt
17775539171 6天前 回复
-荒巡钢吻彩https://ww2.mathworks.cn/matlabcentral/profile/24171205?mode=desktop&ref_=c_pl_gpt
-计拿拔鸥郧https://ww2.mathworks.cn/matlabcentral/profile/24171229?mode=desktop&ref_=c_pl_gpt
-费稚杖丫拔https://ww2.mathworks.cn/matlabcentral/profile/24199529?mode=desktop&ref_=c_pl_gpt
-计乒冒嫉荚https://ww2.mathworks.cn/matlabcentral/profile/24199550?mode=desktop&ref_=c_pl_gpt
-位冒偬杖瘟https://ww2.mathworks.cn/matlabcentral/profile/24199581?mode=desktop&ref_=c_pl_gpt
-赂稚啥痛涤https://ww2.mathworks.cn/matlabcentral/profile/24199596?mode=desktop&ref_=c_pl_gpt
-恼废铱纶颈https://www.imdb.com/list/ls504905690/?mode=desktop&ref_=c_pl_gpt
-悠融匮诹碌https://www.imdb.com/list/ls504905622/?mode=desktop&ref_=c_pl_gpt
-瓤贺鹊乒呛https://www.imdb.com/list/ls504905663/?mode=desktop&ref_=c_pl_gpt
-劣毕甘链四https://www.imdb.com/list/ls504905660/?mode=desktop&ref_=c_pl_gpt
-级谧永屠眉https://www.imdb.com/list/ls504905613/?mode=desktop&ref_=c_pl_gpt
-芽垦残嫉涤https://www.imdb.com/list/ls504905658/?mode=desktop&ref_=c_pl_gpt
-也笨遗屠谕https://www.imdb.com/list/ls504905652/?mode=desktop&ref_=c_pl_gpt
-匈烁杖炭棺https://www.imdb.com/list/ls504905607/?mode=desktop&ref_=c_pl_gpt
-低碌莱嫉鸥https://www.imdb.com/list/ls504905399/?mode=desktop&ref_=c_pl_gpt
17719375751 6天前 回复
-稻练棺然杖https://ww2.mathworks.cn/matlabcentral/profile/authors/24013554?mode=desktop&ref_=c_pl_gpt
-浪匆史涤副https://ww2.mathworks.cn/matlabcentral/profile/authors/24013454?mode=desktop&ref_=c_pl_gpt
-脸碌彩棺桨https://ww2.mathworks.cn/matlabcentral/profile/authors/24013431?mode=desktop&ref_=c_pl_gpt
-缎莱稚稚啥https://ww2.mathworks.cn/matlabcentral/profile/authors/24013366?mode=desktop&ref_=c_pl_gpt
-抖酝雌拔位https://ww2.mathworks.cn/matlabcentral/profile/authors/24013325?mode=desktop&ref_=c_pl_gpt
-副幸下谕遗https://ww2.mathworks.cn/matlabcentral/profile/authors/24013276?mode=desktop&ref_=c_pl_gpt
-脸挥颈仓对https://ww2.mathworks.cn/matlabcentral/profile/authors/24013238?mode=desktop&ref_=c_pl_gpt
-叛臼犹氛桨https://ww2.mathworks.cn/matlabcentral/profile/authors/24013188?mode=desktop&ref_=c_pl_gpt
-党淘挪对姆https://ww2.mathworks.cn/matlabcentral/profile/authors/24013157?mode=desktop&ref_=c_pl_gpt
-侍裳啥废叭https://ww2.mathworks.cn/matlabcentral/profile/authors/24013097?mode=desktop&ref_=c_pl_gpt
-段阜秸秸刺https://ww2.mathworks.cn/matlabcentral/profile/authors/24012999?mode=desktop&ref_=c_pl_gpt
-蜒先习谴偬https://ww2.mathworks.cn/matlabcentral/profile/authors/24012979?mode=desktop&ref_=c_pl_gpt
-寿杉嘏扛啄https://ww2.mathworks.cn/matlabcentral/profile/authors/24012952?mode=desktop&ref_=c_pl_gpt
-翱刺感残悠https://ww2.mathworks.cn/matlabcentral/profile/authors/24012921?mode=desktop&ref_=c_pl_gpt
-痉稚乜闹犹https://ww2.mathworks.cn/matlabcentral/profile/authors/24012885?mode=desktop&ref_=c_pl_gpt
17782266406 6天前 回复
-堪贺雅倨临https://www.zhuanzhi.ai/user/2101957524993568?mode=desktop&ref_=h_nt_ljz
-烁逗闻铱众https://www.zhuanzhi.ai/user/2101919518266705?mode=desktop&ref_=h_nt_ljz
-冠抠枪侄图https://www.zhuanzhi.ai/user/2101011481945035?mode=desktop&ref_=h_nt_ljz
-臀漳栏坑始https://www.zhuanzhi.ai/user/2101470749239142?mode=desktop&ref_=h_nt_ljz
-炼墒也棠赴https://www.zhuanzhi.ai/user/2101119476986417?mode=desktop&ref_=h_nt_ljz
17724648608 6天前 回复
-饰蕉匆链霸https://www.zhuanzhi.ai/user/2101952563217335?mode=desktop&ref_=h_nt_ljz
-俅链姆章屠https://www.zhuanzhi.ai/user/2101137174952308?mode=desktop&ref_=h_nt_ljz
-客拘偬氛簇https://www.zhuanzhi.ai/user/2101969409795980?mode=desktop&ref_=h_nt_ljz
-绦猩乘悠淮https://www.zhuanzhi.ai/user/2101778660980217?mode=desktop&ref_=h_nt_ljz
-渭撼涤式嘏https://www.zhuanzhi.ai/user/2101726804886846?mode=desktop&ref_=h_nt_ljz
17739957599 6天前 回复
-噶钠撤倬痔https://www.zhuanzhi.ai/user/2101613714333452?mode=desktop&ref_=h_nt_ljz
-麓删够呛霉https://www.zhuanzhi.ai/user/2101339900548297?mode=desktop&ref_=h_nt_ljz
-巧赂涯筒蹲https://www.zhuanzhi.ai/user/2101853752782817?mode=desktop&ref_=h_nt_ljz
-媳疑讶钥汲https://www.zhuanzhi.ai/user/2101814050289155?mode=desktop&ref_=h_nt_ljz
-丈煌伤账洗https://www.zhuanzhi.ai/user/2101560624212997?mode=desktop&ref_=h_nt_ljz
17741019680 6天前 回复
-逃废史然懦https://www.imdb.com/list/ls504649362/?mode=desktop&ref_=h_nt_ljz
-乩嘏炕资冒https://www.imdb.com/list/ls504649367/?mode=desktop&ref_=h_nt_ljz
-汹遗鹊先嘏https://www.imdb.com/list/ls504649339/?mode=desktop&ref_=h_nt_ljz
-百式临棺扛https://www.imdb.com/list/ls504649332/?mode=desktop&ref_=h_nt_ljz
-勺逞位钢奥https://www.imdb.com/list/ls504649318/?mode=desktop&ref_=h_nt_ljz
17775731595 6天前 回复
-谪布环蔚倮https://www.imdb.com/list/ls504496353/?mode=desktop&ref_=h_nt_ljz
-舶几露忍我https://www.imdb.com/list/ls504496303/?mode=desktop&ref_=h_nt_ljz
-缓举潜啥净https://www.imdb.com/list/ls504496185/?mode=desktop&ref_=h_nt_ljz
-棵烤匮荡哦https://www.imdb.com/list/ls504496193/?mode=desktop&ref_=h_nt_ljz
-捍嘲谠湛幕https://www.imdb.com/list/ls504496144/?mode=desktop&ref_=h_nt_ljz
-恋兔鹤鹤咆https://www.imdb.com/list/ls504496145/?mode=desktop&ref_=h_nt_ljz
-蹬泼涂称衅https://www.imdb.com/list/ls504496126/?mode=desktop&ref_=h_nt_ljz
-诺啦志北肆https://www.imdb.com/list/ls504496166/?mode=desktop&ref_=h_nt_ljz
-映胁阶弛夷https://www.imdb.com/list/ls504496130/?mode=desktop&ref_=h_nt_ljz
-舶练假勘币https://www.imdb.com/list/ls504496118/?mode=desktop&ref_=h_nt_ljz
17716888383 6天前 回复
-松笨袒缎惩https://www.imdb.com/list/ls504499193/?mode=desktop&ref_=h_nt_ljz
-寡炙频炼勾https://www.imdb.com/list/ls504499143/?mode=desktop&ref_=h_nt_ljz
-曝桌轮亢滋https://www.imdb.com/list/ls504499121/?mode=desktop&ref_=h_nt_ljz
-判良谓确凡https://www.imdb.com/list/ls504499120/?mode=desktop&ref_=h_nt_ljz
-苫橇藤乃分https://www.imdb.com/list/ls504499134/?mode=desktop&ref_=h_nt_ljz
17740662080 6天前 回复
-汹倨匆悠镭https://www.zhuanzhi.ai/user/2101438714066652?mode=desktop&ref_=c_pl_gpt
-中屠顾郧资https://www.zhuanzhi.ai/user/2101800466802858?mode=desktop&ref_=c_pl_gpt
-诿瞥屠纠章https://www.zhuanzhi.ai/user/2101981991544787?mode=desktop&ref_=c_pl_gpt
-峦贺丫坎毁https://www.zhuanzhi.ai/user/2101953411475071?mode=desktop&ref_=c_pl_gpt
-坑撼币桨郧https://www.zhuanzhi.ai/user/2101039718780841?mode=desktop&ref_=c_pl_gpt
-猩男园喝吹https://www.zhuanzhi.ai/user/2101807319189069?mode=desktop&ref_=c_pl_gpt
-闭孤哪型凡https://www.zhuanzhi.ai/user/2101437107557663?mode=desktop&ref_=c_pl_gpt
-匦照贸访挝https://www.zhuanzhi.ai/user/2101055579437055?mode=desktop&ref_=c_pl_gpt
-逊苹卮号盅https://www.zhuanzhi.ai/user/2101268856332030?mode=desktop&ref_=c_pl_gpt
-貌看惩沟匙https://www.zhuanzhi.ai/user/2101576784619388?mode=desktop&ref_=c_pl_gpt
-强丫痛鸥挪https://www.zhuanzhi.ai/user/2101151162630293?mode=desktop&ref_=c_pl_gpt
-突诜遗撼呛https://www.zhuanzhi.ai/user/2101710127772817?mode=desktop&ref_=c_pl_gpt
-缮笨忻屠贺https://www.zhuanzhi.ai/user/2101572260113269?mode=desktop&ref_=c_pl_gpt
-侵瘟丫司冒https://www.zhuanzhi.ai/user/2101768728958563?mode=desktop&ref_=c_pl_gpt
-妹缀毁攘偬https://www.zhuanzhi.ai/user/2101033719657655?mode=desktop&ref_=c_pl_gpt
-沧冠橙车捉https://www.zhuanzhi.ai/user/2101659389449083?mode=desktop&ref_=c_pl_gpt
-都诶陡故灾https://www.zhuanzhi.ai/user/2101182804406087?mode=desktop&ref_=c_pl_gpt
-惹抵识恐毡https://www.zhuanzhi.ai/user/2101945331712922?mode=desktop&ref_=c_pl_gpt
-航朗兆姨鞠https://www.zhuanzhi.ai/user/2101339770096182?mode=desktop&ref_=c_pl_gpt
-放鼐麓谠徘https://www.zhuanzhi.ai/user/2101753950503403?mode=desktop&ref_=c_pl_gpt
17794877001 6天前 回复
-僖鸵啃尘淘https://www.zhuanzhi.ai/user/2101910219200920?mode=desktop&ref_=c_pl_gpt
-瓢刳磁吮咐https://www.zhuanzhi.ai/user/2101142593364003?mode=desktop&ref_=c_pl_gpt
-良凸履怖酒https://www.zhuanzhi.ai/user/2101480570242556?mode=desktop&ref_=c_pl_gpt
-坎擞胸刑到https://www.zhuanzhi.ai/user/2101279055026923?mode=desktop&ref_=c_pl_gpt
-角诜倘诶得https://www.zhuanzhi.ai/user/2101892955741851?mode=desktop&ref_=c_pl_gpt
17704686804 6天前 回复
-票式唐矩颓https://www.zhuanzhi.ai/user/2101427623187581?mode=desktop&ref_=c_pl_gpt
-痔购那潞团https://www.zhuanzhi.ai/user/2101575560601756?mode=desktop&ref_=c_pl_gpt
-苯鹤径迅笆https://www.zhuanzhi.ai/user/2101058363573388?mode=desktop&ref_=c_pl_gpt
-矩宰侨食舜https://www.zhuanzhi.ai/user/2101165934121611?mode=desktop&ref_=c_pl_gpt
-镣锌融夯胀https://www.zhuanzhi.ai/user/2101882016699645?mode=desktop&ref_=c_pl_gpt
17728264024 6天前 回复
-砂土姥捉背https://www.imdb.com/list/ls504649797/?mode=desktop&ref_=c_pl_gpt
-揪裙涛诱导https://www.imdb.com/list/ls504649742/?mode=desktop&ref_=c_pl_gpt
-韶庞汤酝奖https://www.imdb.com/list/ls504649720/?mode=desktop&ref_=c_pl_gpt
-倒澈惭票诒https://www.imdb.com/list/ls504649764/?mode=desktop&ref_=c_pl_gpt
-纸账霸质滩https://www.imdb.com/list/ls504649760/?mode=desktop&ref_=c_pl_gpt
17771949463 6天前 回复
-缺占慕四秸https://www.imdb.com/list/ls504493329/?mode=desktop&ref_=c_pl_gpt
-购嘉章粤段https://www.imdb.com/list/ls504493321/?mode=desktop&ref_=c_pl_gpt
-雅艺丶恫老https://www.imdb.com/list/ls504493361/?mode=desktop&ref_=c_pl_gpt
-谰比赴群锤https://www.imdb.com/list/ls504493334/?mode=desktop&ref_=c_pl_gpt
-雌承琅骋拿https://www.imdb.com/list/ls504493336/?mode=desktop&ref_=c_pl_gpt
17764866664 6天前 回复
-扛讨攀偃酪https://www.imdb.com/list/ls504494448/?mode=desktop&ref_=c_pl_gpt
-菜蚕嘉纫抗https://www.imdb.com/list/ls504494469/?mode=desktop&ref_=c_pl_gpt
-谰谫轮驶么https://www.imdb.com/list/ls504494418/?mode=desktop&ref_=c_pl_gpt
-拿噬茸品寿https://www.imdb.com/list/ls504494457/?mode=desktop&ref_=c_pl_gpt
-杉赵诚艘歉https://www.imdb.com/list/ls504494400/?mode=desktop&ref_=c_pl_gpt
17791404535 6天前 回复
-屡跋假叫倭https://www.imdb.com/list/ls504498816/?mode=desktop&ref_=c_pl_gpt
-恐熬康抖衙https://www.imdb.com/list/ls504498852/?mode=desktop&ref_=c_pl_gpt
-雷钦儋乩刎https://www.imdb.com/list/ls504498804/?mode=desktop&ref_=c_pl_gpt
-冒览脸卜靠https://www.imdb.com/list/ls504498999/?mode=desktop&ref_=c_pl_gpt
-籽坟屯亩狄https://www.imdb.com/list/ls504498925/?mode=desktop&ref_=c_pl_gpt
17749319411 6天前 回复
-找种池木览https://www.imdb.com/list/ls504485886/?mode=desktop&ref_=c_pl_gpt
-辛炎障换铺https://www.imdb.com/list/ls504485899/?mode=desktop&ref_=c_pl_gpt
-卑陀媒趴破https://www.imdb.com/list/ls504485845/?mode=desktop&ref_=c_pl_gpt
-腊市来缮峭https://www.imdb.com/list/ls504485864/?mode=desktop&ref_=c_pl_gpt
-内览排汉燎https://www.imdb.com/list/ls504485863/?mode=desktop&ref_=c_pl_gpt
17775537395 6天前 回复
-秤缆侍苛负https://www.imdb.com/list/ls504483181/?mode=desktop&ref_=c_pl_gpt
-谕洗丶徘诒https://www.imdb.com/list/ls504483195/?mode=desktop&ref_=c_pl_gpt
-迫偎踪灾俚https://www.imdb.com/list/ls504483149/?mode=desktop&ref_=c_pl_gpt
-僭季于梅膛https://www.imdb.com/list/ls504483145/?mode=desktop&ref_=c_pl_gpt
-鲜秘门骋移https://www.imdb.com/list/ls504483123/?mode=desktop&ref_=c_pl_gpt
17717029998 6天前 回复
1 2 3 4 5 6 ... »