bloc控制读写文件
import 'package:flutter/material.dart';
import 'dart:io';
import 'package:path_provider/path_provider.dart';
import 'package:rxdart/rxdart.dart'; main() => runApp(
MyApp(
storage: TextStorage(
bloc: DataBloc()
)
)
); class MyApp extends StatelessWidget {
MyApp({this.storage}); final TextStorage storage; @override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: Text('ass'),),
body: StreamBuilder(
stream: storage.bloc.dataBloc.stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
return Container(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(snapshot.data), RaisedButton(child: Text('add'), onPressed: () {
storage.writeFile('add hello');
},),
RaisedButton(child: Text('check'), onPressed: () {
storage.readFile();
},),
RaisedButton(child: Text('clean'), onPressed: () {
storage.cleanFile();
},),
],
),);
} else {
return Container(child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
child: Text('start'),
onPressed: (){
storage.writeFile('add hello');
}), ],
),
);
}
},
), ),
);
}
} class DataBloc {
PublishSubject dataBloc = PublishSubject(); DataBloc() {
dataBloc.listen(onData);
} void onData(value) {}
} class TextStorage {
TextStorage({this.bloc}); DataBloc bloc; 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();
bloc.dataBloc.add(content);
return content;
} catch (e) {
return '';
}
} Future<File> writeFile(String text) async {
final file = await _localFile;
bloc.dataBloc.add('this is test');
print('get $text, but write: this is test');
return file.writeAsString('$text\r\n', mode: FileMode.append);
} Future<File> cleanFile() async {
final file = await _localFile;
bloc.dataBloc.add('clean');
return file.writeAsString('');
}
}
bloc控制读写文件的更多相关文章
- C++文件操作(输入输出、格式控制、文件打开模式、测试流状态、二进制读写)
1.向文件写数据 头文件#include <ofstream> ①Create an instance of ofstream(创建ofstream实例) ②Open the file w ...
- C#常用IO流与读写文件
.文件系统 ()文件系统类的介绍 文件操作类大都在System.IO命名空间里.FileSystemInfo类是任何文件系统类的基类:FileInfo与File表示文件系统中的文件:Directory ...
- PHP之自定义会话控制---使用文件处理
前三篇简单的总结了下会话控制和文件操作,这一篇说说会话控制的自定义处理方式.既然知道了文件的基本读写,而且在会话控制中,也有人提到,session数据可以保存到缓存或数据库中,实际上当然不会是直接利用 ...
- 你好,C++(5)如何输出数据到屏幕、从屏幕输入数据与读写文件?
2.2 基本输入/输出流 听过HelloWorld.exe的自我介绍之后,大家已经知道了一个C++程序的任务就是描述数据和处理数据.这两大任务的对象都是数据,可现在的问题是,数据不可能无中生有地产生 ...
- C#常用IO流与读写文件 (转)
源自https://www.cnblogs.com/liyangLife/p/4797583.html 谢谢 1.文件系统 (1)文件系统类的介绍 文件操作类大都在System.IO命名空间里.Fil ...
- ASP.NET -- WebForm -- Cookie的使用 应用程序权限设计 权限设计文章汇总 asp.net后台管理系统-登陆模块-是否自动登陆 C# 读写文件摘要
ASP.NET -- WebForm -- Cookie的使用 ASP.NET -- WebForm -- Cookie的使用 Cookie是存在浏览器内存或磁盘上. 1. Test3.aspx文件 ...
- JAVA多线程读写文件范例
在写之前先声明,本文是基于之前在博客园网站上检索到的一份JAVA多线程读写文件的示例,我在写自己的程序时是在那位作者写的基础上做了改良,但已不记得原文的地址.如果有知情者,烦请帖出地址,我在此文上加入 ...
- VC++读写文件
目录 第1章读写文件 1 1.1 API 1 1.2 低级IO 1 1.2.1 文件序号 1 1.2.2 文本文件与二进制文件 1 1.3 流IO 2 1.4 Un ...
- phpspreadsheet 中文文档(六)读写文件+读取文件
2019年10月11日14:05:58 读写文件 从体系结构您已经知道,使用基本PhpSpreadsheet类无法对持久性存储进行读写.为此,PhpSpreadsheet提供读者和作家,这是实现\Ph ...
随机推荐
- Web Service学习(一)
1.WebMethod特性包含哪些属性,都有什么用? 1.BufferResponse属性 该属性表明是否启用对Web Service方法响应的缓冲.当设置为true时,Web Service方法将响 ...
- socket(套接字)初使用
socket层 socket:是应用层与TCP/IP协议通信的中间软件抽象层,是一组接口,在设计模式中,socket其实就是一个门面模式,它把复杂的TCP/IP协议隐藏在socket接口后面. 基于T ...
- C语言面试题分类->宏定义
1.写一个“标准”宏,这个宏输入两个参数并返回较小的一个 答:#define MIN(x, y) ((x)<(y)?(x):(y))//注意x,y要加括号,因为x,y如果有复合运算会出现问题. ...
- raycast 一小段距离碰撞到的poly
dtNavMeshQuery::raycast(dtPolyRef startRef, const float* startPos, const float* endPos, const dtQuer ...
- vue table 固定首列和首行
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name ...
- linux信息收集
1.系统区分debian系列:debian.ubunturedhat系列:redhat.centos 是否为docker.或者为虚拟机 分为通用模块.单独模块的信息获取 2.系统信息收集 内核(是否为 ...
- LeetCode 606 Construct String from Binary Tree 解题报告
题目要求 You need to construct a string consists of parenthesis and integers from a binary tree with the ...
- gitlab自动备份和定时删除
GitLab数据手动备份1.GitLab默认备份目录为/var/opt/gitlab/backups,可以修改/etc/gitlab/gitlab.rb里面的默认存放备份文件目录,这里使用默认备份目录 ...
- [py]python操作zookeeper
参考: https://blog.csdn.net/heizistudio/article/details/79568188 1.安装zookeeper zookeeper-3.4.6.tar.gz ...
- sed memo 2
配置文件注释过滤 示例文件 [user_00@txyun test]$ cat sed_test # comment aaaaaaaaaaa bbbb #comment cccc dddd fffo ...