Flutter 读写本地文件
Posted ajanuw
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Flutter 读写本地文件相关的知识,希望对你有一定的参考价值。
注意
- 安装 path_provider 插件后重启f5, 而不是等待热更新
demo
import 'dart:io';
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Ajnauw',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key}) : super(key: key);
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
String value; // 每次input的值
String allText = ''; // 从本地文件获取的值
/**
* 此方法返回本地文件地址
*/
Future<File> _getLocalFile() async {
// 获取文档目录的路径
Directory appDocDir = await getApplicationDocumentsDirectory();
String dir = appDocDir.path;
final file = new File('$dir/demo.txt');
// print(file);
return file;
}
/**
* 保存value到本地文件里面
*/
void _saveValue() async {
try {
File f = await _getLocalFile();
iosink slink = f.openWrite(mode: FileMode.append);
slink.write('$value
');
// await fs.writeAsString('$value');
setState(() {
value = '';
});
slink.close();
} catch (e) {
// 写入错误
print(e);
}
}
/**
* 读取本地文件的内容
*/
void _readContent() async {
File file = await _getLocalFile();
// 从文件中读取变量作为字符串,一次全部读完存在内存里面
String contents = await file.readAsString();
setState(() {
allText = contents;
});
}
// 清空本地保存的文件
void _clearContent() async {
File f = await _getLocalFile();
await f.writeAsString('');
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('demo'),
),
body: ListView(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
child: Row(
children: <Widget>[
Expanded(
child: Container(
margin: EdgeInsets.only(right: 16.0),
child: TextField(
controller: TextEditingController(
text: value,
),
onChanged: (String v) {
value = v;
},
onSubmitted: (String r) {
value = r;
},
),
),
),
RaisedButton(
color: Theme.of(context).primaryColor,
onPressed: _saveValue,
child: Text('保存'),
),
],
),
),
Container(
child: Column(
children: <Widget>[
Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
RaisedButton(
color: Theme.of(context).accentColor,
onPressed: _readContent,
child: Text('获取本地数据'),
),
RaisedButton(
color: Colors.red,
textColor: Colors.white,
onPressed: _clearContent,
child: Text('清空本地数据'),
),
],
),
Container(
padding: const EdgeInsets.all(16.0),
child: Text('''$allText'''),
),
],
),
),
],
),
);
}
}
以上是关于Flutter 读写本地文件的主要内容,如果未能解决你的问题,请参考以下文章
错误记录Flutter 混合开发获取 BinaryMessenger 报错 ( FlutterActivityAndFragmentDelegate.getFlutterEngine() )(代码片段
在 webview_flutter 中启用捏合和缩放,在哪里添加代码片段 [this.webView.getSettings().setBuiltInZoomControls(true);]