import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:async';
import 'dart:io'; void main() {
runApp(
MaterialApp(
title: 'Read/Write Files',
home: MyApp(storage: TextStorage()),
),
);
} class TextStorage {
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
} Future<File> get _localFile async {
final path = await _localPath;
return File('$path/text.txt');
} Future<String> readFile() async {
try {
final file = await _localFile; String content = await file.readAsString();
return content;
} catch (e) {
return '';
}
} Future<File> writeFile(String text) async {
final file = await _localFile;
return file.writeAsString('$text\r\n', mode: FileMode.append);
} Future<File> cleanFile() async {
final file = await _localFile;
return file.writeAsString('');
}
} class MyApp extends StatefulWidget {
final TextStorage storage; MyApp({Key key, @required this.storage}) : super(key: key); @override
_MyAppState createState() => _MyAppState();
} class _MyAppState extends State<MyApp> {
TextEditingController _textField = new TextEditingController(); String _content = ''; @override
void initState() {
super.initState();
widget.storage.readFile().then((String text) {
setState(() {
_content = text;
});
});
} Future<File> _writeStringToTextFile(String text) async {
setState(() {
_content += text + '\r\n';
}); return widget.storage.writeFile(text);
} Future<File> _clearContentsInTextFile() async {
setState(() {
_content = '';
}); return widget.storage.cleanFile();
} @override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Read/Write File Example'),
backgroundColor: Colors.blue,
),
body: Container(
padding: EdgeInsets.all(20.0),
child: Column(
children: <Widget>[
TextField(
controller: _textField,
),
Padding(
padding: EdgeInsets.all(20.0),
child: RaisedButton(
child: Text('Write to File'),
onPressed: () {
if (_textField.text.isNotEmpty) {
_writeStringToTextFile(_textField.text);
_textField.clear();
}
},
),
),
Padding(
padding: EdgeInsets.only(bottom: 20.0),
child: RaisedButton(
child: Text(
'Clear Contents',
style: TextStyle(color: Colors.white),
),
color: Colors.redAccent,
onPressed: () {
_clearContentsInTextFile();
},
),
),
Expanded(
flex: 1,
child: new SingleChildScrollView(
child: Text(
'$_content',
style: TextStyle(
color: Colors.blue,
fontSize: 22.0,
),
),
),
),
],
),
),
);
}
}

  

记得家权限.

flutter 读写文件的更多相关文章

  1. Hyper-V无法文件拖拽解决方案~~~这次用一个取巧的方法架设一个FTP来访问某个磁盘,并方便的读写文件

    异常处理汇总-服 务 器 http://www.cnblogs.com/dunitian/p/4522983.html 服务器相关的知识点:http://www.cnblogs.com/dunitia ...

  2. 计算机程序的思维逻辑 (60) - 随机读写文件及其应用 - 实现一个简单的KV数据库

    57节介绍了字节流, 58节介绍了字符流,它们都是以流的方式读写文件,流的方式有几个限制: 要么读,要么写,不能同时读和写 不能随机读写,只能从头读到尾,且不能重复读,虽然通过缓冲可以实现部分重读,但 ...

  3. Python读写文件

    Python读写文件1.open使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件. file_object = open('t ...

  4. php中并发读写文件冲突的解决方案

    在这里提供4种高并发读写文件的方案,各有优点,可以根据自己的情况解决php并发读写文件冲突的问题. 对于日IP不高或者说并发数不是很大的应用,一般不用考虑这些!用一般的文件操作方法完全没有问题.但如果 ...

  5. C#读写文件的方法汇总_C#教程_脚本之家

    C#读写文件的方法汇总_C#教程_脚本之家 http://www.jb51.net/article/34936.htm

  6. Inno Setup 如何读写文件

    软件安装的实质就是拷贝,对于简单的打包当然不需要考虑修改某(配置)文件.通过inno修改文件的目的在于把安装时相关信息写入文件中,提供其它应用的读取,而这些信息也只能在安装时才能确定,比如安装用户选择 ...

  7. java使用IO读写文件总结

    每次用到IO的读写文件都老忘记写法,都要翻过往笔记,今天总结下,省的以后老忘.java读写文件的IO流分两大类,字节流和字符流,基类分别是字符:Reader和Writer:字节:InputStream ...

  8. python读写文件时中文的转码问题

    读写文件都要将中文转为unicode字符. 读文件: u = unicode(s, 'gbk') 这里不能使用encode 写文件: u = encode('utf')

  9. JAVA基础学习之流的简述及演示案例、用缓冲区方法buffer读写文件、File类对象的使用、Serializable标记接口(6)

    1.流的简述及演示案例输入流和输出流相对于内存设备而言.将外设中的数据读取到内存中:输入将内存的数写入到外设中:输出.字符流的由来:其实就是:字节流读取文字字节数据后,不直接操作而是先查指定的编码表. ...

随机推荐

  1. <c:forEach var="role" items="[entity.Role@d54d4d, entity.Role@1c61868, entity.Role@6c58db, entity.Role@13da8a5]"> list 集合数据转换异常

    <c:forEach var="role" items="[entity.Role@d54d4d, entity.Role@1c61868, entity.Role ...

  2. 好大一个坑: EF Core 异步读取大字符串字段比同步慢100多倍

    这两天遇到一个奇怪的问题,通过 EF/EF Core 查询数据库速度奇慢,先是在传统的 ASP.NET 项目中遇到(用的是EF6.0),后来将该项目迁移至 ASP.NET Core 也是同样的问题(用 ...

  3. Jmeter-----参数配置

    参数化配置: 设置为3个线程后,三个用户均能运行

  4. linux CentOS 安装 nginx+tomcat+java+mysql运行环境

    本文介绍了CentOS7 64 Java,Tomcat,MySQL,Maven热部署等服务器环境的搭建过程. 服务器: 已经将所需要的工具(Xshell,Xftp.FileZilla等sftp上传工具 ...

  5. RF - selenium - 常用关键字 - 示例

    1. 打开浏览器 Open Browser    http://www.baidu.com    chrome 2. 关闭浏览器 Close Browsers Close All Browser 3. ...

  6. 从手机中提取boot.img

    测试环境:nexus 5,android 6.0 怕出问题可以先用TWRP备份 adb shell su cd /dev/block/platform/msm_sdcc./by-name ls -l ...

  7. ThinkPHP设计模式与Trait技术

    阅读原文 设计模式 单例模式 class Site { //属性 public $siteName; //本类的静态实例 protected static $instance = null; //禁用 ...

  8. btcpool之GbtMaker

    一.简介 GbtMaker全称getblocktemplate maker,它通过getblocktemplate rpc接口从bitcoind获得挖矿所需数据,然后把该数据发送到kafka消息队列. ...

  9. pwn学习日记Day1 基础知识积累

    ida / od 窗口(针对od操作) 反汇编窗口:显示被调试程序的反汇编代码,标题栏上的地址.HEX 数据.反汇编.注释可以通过在窗口中右击出现的菜单 界面选项->隐藏标题 或 显示标题 来进 ...

  10. 五、latex文档的篇章结构