flutter 读写文件

不管写文件还是读取文件,首先得有一个可供读写操作的文件才行,那么如何得到一个可供读写的文件呢?请用下面这个方法:
Future<File> getFile() async {

// 获取应用目录
String dir = (await getApplicationDocumentsDirectory()).path;
return File('$dir/hi.txt');
}


// 读取指定文件的内容,读文件的前提是获取可以用于读写操作的文件;
Future<String> readFile() async {
try {
File file = await getFile();//getFile()是一个Future函数,所以用await,赋值对象是File类型;
String contents = await file.readAsString();//file.readAsString()是一个Future类型函数,用await修饰,然后赋值给了字符串;
return contents;
} on FileSystemException {
return 'no data';
}
}



Future<int> writeFile()async{
try {
await (await getFile()).writeAsString('concer the world!');//注意这里的两个await,括号里面的函数getFile()是Future类型,所以用await修饰,writeAsString也是Future类型,也需要用await修饰,故此有两个await;
print('write had been done');
return 1;
} on FileSystemException {
return 0;
}
}

以下是一个完整的示例:
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';

void main()=>runApp(MyApp());

class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: HomePage(),
);
}
}

class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
TextEditingController textEditingController;
TextEditingController displayTextEditingController;
Future<File> getFile() async {
// 获取应用目录
String dir = (await getApplicationDocumentsDirectory()).path;
return File('$dir/hi.txt');
}
// 读取指定文件的内容,读文件的前提是获取可以用于读写操作的文件;
Future<String> readFile() async {
try {
File file = await getFile();
String contents = await file.readAsString();
return contents;
} on FileSystemException {
return 'no data';
}
}

Future<int> writeFile(data)async{
try {
await (await getFile()).writeAsString(data);
print(data);
return 1;
} on FileSystemException {
return 0;
}
}
@override
void initState() {
// TODO: implement initState
textEditingController = TextEditingController();
displayTextEditingController = TextEditingController();
super.initState();
}
@override
void dispose() {
// TODO: implement dispose
textEditingController.dispose();
displayTextEditingController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: ListView(
children: <Widget>[
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: textEditingController,
decoration: InputDecoration(
labelText: '请在此处大放厥词',
contentPadding: EdgeInsets.all(18),
fillColor: Colors.orange,
filled: true,
),
),
),
InkWell(
onTap: (){
writeFile(textEditingController.text);
},
child: Padding(
padding: const EdgeInsets.only(left: 68,right: 68),
child: Container(
child: Center(child: Text('点击这里,大放厥词写入文件')),
decoration: BoxDecoration(
borderRadius: BorderRadius.all(Radius.circular(28)),
color: Colors.red,
),
width: 28,
height: 58,
),
),
),
Padding(
padding: const EdgeInsets.all(8.0),
child: TextField(
controller: displayTextEditingController,
decoration: InputDecoration(
labelText: '这里是从文件读取的你的大放厥词',
contentPadding: EdgeInsets.all(18),
fillColor: Colors.orange,
filled: true,
),
),
),
Padding(
padding: EdgeInsets.only(left:88.0,right: 88.0,),
child: RaisedButton(
child: Text('读取文件'),
color: Colors.red,
onPressed: (){
var data = readFile();
data.then((value){
displayTextEditingController.text = value;
setState(() {
});
});
}),
),
],
),
);
}
}


posted @ 2019-07-06 21:03  braveheart007  阅读(1217)  评论(0编辑  收藏  举报