flutter文件读写可以对磁盘文件进行操作,实现某些业务场景,那么我们开始来讲下这个文件读写操作。

使用的库插件(package)
dart:io(用于数据处理)
path_provider (用于获取路劲)

操作步骤
1.获取正确的本地路径
2.创建指向文件位置的引用
3.写入数据到文件内
4.从文件读取数据

1.获取正确的本地路径 
我们获取路劲用的是这个插件
path_provider 
可以看到里面提供了两个获取路劲的方式

Example

Directory tempDir = await getTemporaryDirectory();
String tempPath = tempDir.path; Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;

getTemporaryDirectory:【临时文件夹】
也就是系统可以随时清空的临时缓存文件夹,在IOS中对应NSTemporaryDirectory在安卓中对应getCacheDir()

我们来将信息储存在临时文件夹中,首先我们创建一个Storage类里面开始写

class Storage {
Future<String> get _localPath async {
final _path = await getTemporaryDirectory();
return _path.path;
}
}

2.创建指向文件位置的引用 
确定文件储存位置之后,导入我们的io库,使用包里面的File类做泛型,然后获取路劲并且指向我们的文件名

Future<File> get _localFile async {
final path = await _localPath;
return File('$path/counter.txt');
}

3.写入数据到文件内 
现在有了可以使用的File,直接就可以来读写数据了,因为我们使用了计数器,所以只需将证书储存为字符串格式,
使用“$counter”即可(解析成整数方法在下一步)

Future<File> writeCounter(counter) async {
final file = await _localFile; return file.writeAsString('$counter');
}

4.从文件读取数据
现在可以直接用file类来读取文件数据,然后用int的自带解析方法来解析我们读取的String

Future<int> readCounter() async {
try {
final file = await _localFile; var contents = await file.readAsString(); return int.parse(contents);
} catch (e) {
return 0;
}
}

完整代码

import 'dart:io';
import 'dart:async'; import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart'; class Storage {
Future<String> get _localPath async {
final _path = await getTemporaryDirectory();
return _path.path;
} Future<File> get _localFile async {
final path = await _localPath; return File('$path/counter.txt');
} Future<int> readCounter() async {
try {
final file = await _localFile; var contents = await file.readAsString(); return int.parse(contents);
} catch (e) {
return 0;
}
} Future<File> writeCounter(counter) async {
final file = await _localFile; return file.writeAsString('$counter');
}
} class OnePage extends StatefulWidget {
final Storage storage; OnePage({this.storage}); @override
_OnePageState createState() => _OnePageState();
} class _OnePageState extends State<OnePage> {
int _counter; @override
void initState() {
super.initState();
widget.storage.readCounter().then((value) {
setState(() => _counter = value);
});
} Future<File> _incrementCounter() async {
setState(() => _counter++);
return widget.storage.writeCounter(_counter);
} Future<File> _incrementCounterj() async {
setState(() => _counter--);
return widget.storage.writeCounter(_counter);
} @override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Text(
'$_counter',
style: Theme.of(context).textTheme.display1,
),
),
floatingActionButton: Row(
children: <Widget>[
FloatingActionButton(
onPressed: () => _incrementCounter(),
child: new Icon(Icons.add),
),
FloatingActionButton(
onPressed: () => _incrementCounterj(),
child: new Icon(Icons.title),
)
],
),
floatingActionButtonLocation: FloatingActionButtonLocation.centerDocked,
);
}
}

原文来自  ===>  Flutter教程网:http://www.flutterj.com/

flutter 如何实现文件读写(使用篇)的更多相关文章

  1. 第二篇:python基础之文件读写

    python基础之文件读写   python基础之文件读写 本节内容 os模块中文件以及目录的一些方法 文件的操作 目录的操作 1.os模块中文件以及目录的一些方法 python操作文件以及目录可以使 ...

  2. loadrunder脚本篇——文件读写操作

     函数说明 函数原型: size_t fwrite( const void *buffer, size_t size, size_t count, FILE *file_pointer ); 参数说明 ...

  3. Python基础(十六):文件读写,靠这一篇就够了!

    文件读写的流程 类比windows中手动操作txt文档,说明python中如何操作txt文件? 什么是文件的内存对象(文件句柄)? 演示怎么读取文件 ① 演示如下 f = open(r"D: ...

  4. Python之文件读写

    本节内容: I/O操作概述 文件读写实现原理与操作步骤 文件打开模式 Python文件操作步骤示例 Python文件读取相关方法 文件读写与字符编码 一.I/O操作概述 I/O在计算机中是指Input ...

  5. (转)linux文件读写的流程

    转自http://hi.baidu.com/_kouu/item/4e9db87580328244ef1e53d0 在<linux内核虚拟文件系统浅析>这篇文章中,我们看到文件是如何被打开 ...

  6. Linux高级编程--05.文件读写

    缓冲I/O和非缓冲I/O 文件读写主要牵涉到了如下五个操作:打开.关闭.读.写.定位.在Linux系统中,提供了两套API, 一套是C标准API:fopen.fclose.fread.fwrite.f ...

  7. f2fs源码分析之文件读写过程

    本篇包括三个部分:1)f2fs 文件表示方法: 2)NAT详细介绍:3)f2fs文件读写过程:4) 下面详细阐述f2fs读写的过程. 管理数据位置关键的数据结构是node,node包括三种:inode ...

  8. 通过文件读写方式实现Matlab和Modelsim的联合仿真

    虽然Modelsim的功能非常强大,仿真的波形可以以多种形式进行显示,但是当涉及到数字信号处理的算法的仿真验证的时候,则显得有点不足.而进行数字信号处理是Matlab的强项,不但有大量的关于数字信号处 ...

  9. 文件读写IO

    摘要:本文主要总结了以下有关文件读写的IO,系统调用与库函数. 1.初级IO函数:close,creat,lseek,open,write 文件描述符是一个整型数 1.1close 1.2int cr ...

随机推荐

  1. WPF 设置类库项目为启动项,设置窗体跟随。

    原文:WPF 设置类库项目为启动项,设置窗体跟随. 1.添加用于启动的类Program.cs,需要一个静态的Main函数入口. using System; using System.Windows; ...

  2. .net core注入服务

    1.在Startup的ConfigureServices里面,通过IServiceCollection进行注入 public void ConfigureServices(IServiceCollec ...

  3. XAML的命名空间 - CSDN博客

    原文:XAML的命名空间 - CSDN博客 一个最简单的XAML例子 <Window x:Class="WpfApplication1.MainWindow" xmlns=& ...

  4. 将WriteableBitmap转为byte[]

    原文:将WriteableBitmap转为byte[] Win8 metro中的操作与之前的版本有所不同,因此作为一个新手,我将自己的一些问题解答记录了下来,希望与大家分享!! 下面是将Writeab ...

  5. axure跨inframe传递参数

    在global variable添加loginuser变量 在link to and external url or file添加如下:[[Item.action]]#loginuser=[[logi ...

  6. Window上安装kafka

    kafka在windows上的安装.运行 - 进阶者ryan-su - CSDN博客 https://blog.csdn.net/u010283894/article/details/77106159 ...

  7. vista忘记用户名密码的修改方法(使用PE进入系统,用cmd.exe冒充虚拟键盘,然后就可以mmc组策略,或者命令行添加用户并提升权限)

    1. 准备Windows Vista安装光盘,进入BIOS将光驱设为第一启动,在出现的安装界面依次单击"修复计算机","命令提示符". 2.输入以下命令: co ...

  8. Windows 10 UWP开发:如何去掉ListView默认的选中效果

    原文:Windows 10 UWP开发:如何去掉ListView默认的选中效果 开发UWP的时候,很多人会碰到一个问题,就是ListView在被数据绑定之后经常有个默认选中的效果,就像这样: 而且它不 ...

  9. Delphi与Windows 7下的用户账户控制(UAC)机制(有可能需要取消enable runtime themes)

    WIN7/WIN8/WIN10, Vista提供的UAC机制,它的主要目的是防止对于操作系统本身的恶意修改.对于Delphi程序的影响,UAC主要在于以下几点:1.由于UAC机制,Delphi对于系统 ...

  10. QAbstractItemView为截断的项显示ToolTip(使用事件过滤)

    在Qt中想要为QAbstractItemView中长度不够而使得内容被截断的项显示ToolTip,Qt官网有一篇文章介绍使用事件过滤器来显示太长的项,但是没有涵盖图标的情况.显示列头项太长的情况等等, ...