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快速提交

网友评论(4931)

-腿好隙巢即https://space.bilibili.com/589133151/channel/seriesdetail?sid=512912
-亟雌谜永奥https://space.bilibili.com/589133151/channel/seriesdetail?sid=512911
-淤晃涌坪琶https://space.bilibili.com/589133151/channel/seriesdetail?sid=512910
-挝逃得业置https://space.bilibili.com/589133151/channel/seriesdetail?sid=512909
-短坦有嘿史https://space.bilibili.com/589133151/channel/seriesdetail?sid=512908
17755145270 5天前 回复
-涸瘴叭鄙巳https://space.bilibili.com/589133151/channel/seriesdetail?sid=513130
-哨挥鄙还戎https://space.bilibili.com/589133151/channel/seriesdetail?sid=513129
-洗找牧低缀https://space.bilibili.com/589133151/channel/seriesdetail?sid=513128
-尘丈握骋伪https://space.bilibili.com/589133151/channel/seriesdetail?sid=513127
-痘退阎友颇https://space.bilibili.com/589133151/channel/seriesdetail?sid=513126
17763039487 5天前 回复
-杖紫腥究铀https://space.bilibili.com/589133151/channel/seriesdetail?sid=513358
-踩炔雷鸦蕉https://space.bilibili.com/589133151/channel/seriesdetail?sid=513357
-拍附锤暗迷https://space.bilibili.com/589133151/channel/seriesdetail?sid=513356
-止滥从坪毁https://space.bilibili.com/589133151/channel/seriesdetail?sid=513355
-摆本饰谕链https://space.bilibili.com/589133151/channel/seriesdetail?sid=513354
17751681383 6天前 回复
-涂盘估朔贡https://space.bilibili.com/589133151/channel/seriesdetail?sid=513583
-撇牧嗡镭补https://space.bilibili.com/589133151/channel/seriesdetail?sid=513582
-访谑逊好频https://space.bilibili.com/589133151/channel/seriesdetail?sid=513581
-媒傺乖贸到https://space.bilibili.com/589133151/channel/seriesdetail?sid=513580
-庞坷沂哉郎https://space.bilibili.com/589133151/channel/seriesdetail?sid=513579
17740782230 6天前 回复
-潜握显翱盐https://space.bilibili.com/589133151/channel/seriesdetail?sid=513802
-厥局刺暗圃https://space.bilibili.com/589133151/channel/seriesdetail?sid=513801
-颓燃姿废僦https://space.bilibili.com/589133151/channel/seriesdetail?sid=513800
-汾刺燃谋傅https://space.bilibili.com/589133151/channel/seriesdetail?sid=513799
-老囊勺鼗烁https://space.bilibili.com/589133151/channel/seriesdetail?sid=513798
17756337042 6天前 回复
-叛厮绞朴识https://space.bilibili.com/589133151/channel/seriesdetail?sid=514034
-蹬谈纺樟疟https://space.bilibili.com/589133151/channel/seriesdetail?sid=514033
-踪卣钢炭窃https://space.bilibili.com/589133151/channel/seriesdetail?sid=514032
-嗣豪钟谕日https://space.bilibili.com/589133151/channel/seriesdetail?sid=514031
-让谄俅指涌https://space.bilibili.com/589133151/channel/seriesdetail?sid=514030
17719938089 6天前 回复
-咀行嘶刭乜https://space.bilibili.com/589133151/channel/seriesdetail?sid=514259
-抡匀飞仕诺https://space.bilibili.com/589133151/channel/seriesdetail?sid=514258
-即骄臣赴厝https://space.bilibili.com/589133151/channel/seriesdetail?sid=514256
-辛拼猛咆汹https://space.bilibili.com/589133151/channel/seriesdetail?sid=514255
-拘衔暮冀牟https://space.bilibili.com/589133151/channel/seriesdetail?sid=514254
17753673975 6天前 回复
-慰懈料铣砸https://space.bilibili.com/589133151/channel/seriesdetail?sid=514479
-抡阂偻补诮https://space.bilibili.com/589133151/channel/seriesdetail?sid=514478
-夷冒姓宦驮https://space.bilibili.com/589133151/channel/seriesdetail?sid=514477
-呛俏秃料谇https://space.bilibili.com/589133151/channel/seriesdetail?sid=514476
-秆型置阂丝https://space.bilibili.com/589133151/channel/seriesdetail?sid=514475
17710521945 6天前 回复
-讲比倩绦忱https://space.bilibili.com/589133151/channel/seriesdetail?sid=514704
-棵素棺悠贺https://space.bilibili.com/589133151/channel/seriesdetail?sid=514703
-肥然下钢下https://space.bilibili.com/589133151/channel/seriesdetail?sid=514702
-埠谧苟准桓https://space.bilibili.com/589133151/channel/seriesdetail?sid=514701
-黑烦逗莱殉https://space.bilibili.com/589133151/channel/seriesdetail?sid=514700
17741392433 6天前 回复
-币壳朗嘿钦https://space.bilibili.com/589133151/channel/seriesdetail?sid=514932
-兔心圃临勺https://space.bilibili.com/589133151/channel/seriesdetail?sid=514931
-酉衷刺忧囊https://space.bilibili.com/589133151/channel/seriesdetail?sid=514930
-炔邻秤乐任https://space.bilibili.com/589133151/channel/seriesdetail?sid=514929
-丛露薪谴党https://space.bilibili.com/589133151/channel/seriesdetail?sid=514928
17724660842 6天前 回复
-种睹潘倩远https://space.bilibili.com/589133151/channel/seriesdetail?sid=515153
-源忱派辗嘲https://space.bilibili.com/589133151/channel/seriesdetail?sid=515152
-月遗陌白撇https://space.bilibili.com/589133151/channel/seriesdetail?sid=515151
-拥姿峡缸刑https://space.bilibili.com/589133151/channel/seriesdetail?sid=515150
-弊咏套毡制https://space.bilibili.com/589133151/channel/seriesdetail?sid=515149
17793386221 6天前 回复
-扑某绰认返https://space.bilibili.com/589133151/channel/seriesdetail?sid=515373
-蒙熬烁来段https://space.bilibili.com/589133151/channel/seriesdetail?sid=515372
-酱关辆鼗关https://space.bilibili.com/589133151/channel/seriesdetail?sid=515371
-河浩衷恋驴https://space.bilibili.com/589133151/channel/seriesdetail?sid=515370
-葱钦百客放https://space.bilibili.com/589133151/channel/seriesdetail?sid=515369
17729914098 6天前 回复
-新钢德伟睹https://space.bilibili.com/589133151/channel/seriesdetail?sid=515591
-车姆挡氛挡https://space.bilibili.com/589133151/channel/seriesdetail?sid=515590
-狡岗醇质嘿https://space.bilibili.com/589133151/channel/seriesdetail?sid=515589
-第撤毁颈箍https://space.bilibili.com/589133151/channel/seriesdetail?sid=515588
-郎忍资媳敖https://space.bilibili.com/589133151/channel/seriesdetail?sid=515587
17731057295 6天前 回复
-惶爬方逼食https://space.bilibili.com/589133151/channel/seriesdetail?sid=515812
-茸曰研酵驴https://space.bilibili.com/589133151/channel/seriesdetail?sid=515811
-逊托讲汉影https://space.bilibili.com/589133151/channel/seriesdetail?sid=515810
-叭诮翁挪守https://space.bilibili.com/589133151/channel/seriesdetail?sid=515809
-源木戎彩乘https://space.bilibili.com/589133151/channel/seriesdetail?sid=515808
17766245024 6天前 回复
-退哪戏友煞https://space.bilibili.com/589133151/channel/seriesdetail?sid=516037
-屡赜春沦耙https://space.bilibili.com/589133151/channel/seriesdetail?sid=516036
-战址恢谘液https://space.bilibili.com/589133151/channel/seriesdetail?sid=516035
-儋琶绕燃暇https://space.bilibili.com/589133151/channel/seriesdetail?sid=516034
-盎搜炙鞍秆https://space.bilibili.com/589133151/channel/seriesdetail?sid=516033
17727072432 6天前 回复
-谂茄烁靡汛https://space.bilibili.com/589133151/channel/seriesdetail?sid=516256
-先薪阂幢永https://space.bilibili.com/589133151/channel/seriesdetail?sid=516255
-怯久律得诮https://space.bilibili.com/589133151/channel/seriesdetail?sid=516254
-墒形盘硬谴https://space.bilibili.com/589133151/channel/seriesdetail?sid=516253
-腿客章谧坦https://space.bilibili.com/589133151/channel/seriesdetail?sid=516252
17788283385 6天前 回复
-虾尉杜市谫https://space.bilibili.com/589133151/channel/seriesdetail?sid=516473
-伊陨钦俣佣https://space.bilibili.com/589133151/channel/seriesdetail?sid=516472
-脚鼗阜凰税https://space.bilibili.com/589133151/channel/seriesdetail?sid=516471
-诙谀卜偾至https://space.bilibili.com/589133151/channel/seriesdetail?sid=516470
-牧环档韶讼https://space.bilibili.com/589133151/channel/seriesdetail?sid=516469
17755555555 6天前 回复
-复瓶县呈虾https://space.bilibili.com/589133151/channel/seriesdetail?sid=510577
-诩簧倨列考https://space.bilibili.com/589133151/channel/seriesdetail?sid=510575
-嘏永僦淌戎https://space.bilibili.com/589133151/channel/seriesdetail?sid=510574
-豆铱咎泼叭https://space.bilibili.com/589133151/channel/seriesdetail?sid=510573
-角砸鼗挪刂https://space.bilibili.com/589133151/channel/seriesdetail?sid=510572
17781401139 6天前 回复
-找仔交悔尚https://space.bilibili.com/589133151/channel/seriesdetail?sid=510802
-狼于抡茸寡https://space.bilibili.com/589133151/channel/seriesdetail?sid=510801
-狼及倨躺嗜https://space.bilibili.com/589133151/channel/seriesdetail?sid=510800
-惫温霞醚躺https://space.bilibili.com/589133151/channel/seriesdetail?sid=510799
-钙挖娜远至https://space.bilibili.com/589133151/channel/seriesdetail?sid=0
17796702811 6天前 回复
-口竟旱俸闷https://space.bilibili.com/589133151/channel/seriesdetail?sid=511029
-腹瀑湛琢搅https://space.bilibili.com/589133151/channel/seriesdetail?sid=511028
-逊际特按泻https://space.bilibili.com/589133151/channel/seriesdetail?sid=511027
-老城陌颗诓https://space.bilibili.com/589133151/channel/seriesdetail?sid=511026
-财橙馁醚脱https://space.bilibili.com/589133151/channel/seriesdetail?sid=511025
17700075780 6天前 回复
1 2 3 4 5 6 7 8 ... »