Flutter的数据存储分为三类
- Preferences
- 文件存储
- 数据库
Preferences
Preference相当于iOS的NSUserDefaults,其实也是按plist的方式存储的
step1:添加依赖
# 添加sharedPreference依赖
shared_preferences: ^0.5.0
step2:pub get
step3:导入头文件
import 'package:shared_preferences/shared_preferences.dart';
使用方法:
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
// 创建一个新路由
class shared_preferences_Route extends StatefulWidget {
@override // 重写
State<StatefulWidget> createState() => StorageState();
}
class StorageState extends State {
var _textFieldController = new TextEditingController();
var _storageString = '';
final STORAGE_KEY = 'storage_key';
/**
* 利用SharedPreferences存储数据
*/
Future saveString() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
sharedPreferences.setString(
STORAGE_KEY, _textFieldController.value.text.toString());
}
/**
* 获取存在SharedPreferences中的数据
*/
Future getString() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
setState(() {
_storageString = sharedPreferences.get(STORAGE_KEY);
});
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('Shared Preferences存储'),
),
body: new Column(
children: <Widget>[
Text("请输入文本", textAlign: TextAlign.center),
TextField(
controller: _textFieldController,
),
MaterialButton(
onPressed: saveString,
child: new Text("存储"),
color: Colors.pink,
),
MaterialButton(
onPressed: getString,
child: new Text("获取"),
color: Colors.lightGreen,
),
Text('shared_preferences存储的值为 $_storageString'),
],
),
);
}
}
文件存储
在path_provider中有三个获取文件路径的方法:
- getTemporaryDirectory()
://获取应用缓存目录,等同iOS的NSTemporaryDirectory()和Android的getCacheDir() 方法。
- getApplicationDocumentsDirectory():
//获取应用文件目录类似于iOS的NSDocumentDirectory和Android上的 AppData目录。
step1:添加依赖
# 添加文件依赖
path_provider: ^0.5.0
step2:pub get
step3:导入头文件
import 'package:path_provider/path_provider.dart';
import 'dart:io';
使用方法:
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';
// 创建一个新路由
class file_Route extends StatefulWidget {
@override // 重写
State<StatefulWidget> createState() => StorageState();
}
class StorageState extends State {
var _textFieldController = new TextEditingController();
var _storageString = '';
/**
* 利用文件存储数据
*/
saveString() async {
final file = await getFile('file.text');
//写入字符串
file.writeAsString(_textFieldController.value.text.toString());
}
/**
* 获取存在文件中的数据
*/
Future getString() async {
final file = await getFile('file.text');
var filePath = file.path;
setState(() {
file.readAsString().then((String value) {
_storageString = value +'\n文件存储路径:'+filePath;
});
});
}
/**
* 初始化文件路径
*/
Future<File> getFile(String fileName) async {
//获取应用文件目录类似于Ios的NSDocumentDirectory和Android上的 AppData目录
final fileDirectory = await getApplicationDocumentsDirectory();
//获取存储路径
final filePath = fileDirectory.path;
//或者file对象(操作文件记得导入import 'dart:io')
return new File(filePath + "/"+fileName);
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('文件存储'),
),
body: new Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text("文件存储", textAlign: TextAlign.center),
TextField(
controller: _textFieldController,
),
MaterialButton(
onPressed: saveString,
child: new Text("存储"),
color: Colors.cyan,
),
MaterialButton(
onPressed: getString,
child: new Text("获取"),
color: Colors.deepOrange,
),
Text('从文件存储中获取的值为 $_storageString'),
],
),
);
}
}
网友评论