博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
bloc控制读写文件
阅读量:4319 次
发布时间:2019-06-06

本文共 2810 字,大约阅读时间需要 9 分钟。

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: 
[ 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:
[ 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
get _localPath async { final directory = await getApplicationDocumentsDirectory(); return directory.path; } Future
get _localFile async { final path = await _localPath; return File('$path/text.txt'); } Future
readFile() async { try { final file = await _localFile; String content = await file.readAsString(); bloc.dataBloc.add(content); return content; } catch (e) { return ''; } } Future
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
cleanFile() async { final file = await _localFile; bloc.dataBloc.add('clean'); return file.writeAsString(''); }}

  

转载于:https://www.cnblogs.com/pythonClub/p/10686442.html

你可能感兴趣的文章
HDU1518 Square 【剪枝】
查看>>
桥接模式
查看>>
android windows 上JNI编程
查看>>
PHP中可变变量到底有什么用?
查看>>
谈一谈最近关闭的Kindle人论坛
查看>>
android java 与C 通过 JNI双向通信
查看>>
javascript:另一种图片滚动切换效果思路
查看>>
获取css的属性值
查看>>
Win32_NetworkAdapterConfiguration
查看>>
Flash:DisplayObject的transform/matrix的潜规则、小bug
查看>>
方维系统常用的jquery库以及各个库的含义
查看>>
[LeetCode]101. Symmetric Tree
查看>>
Node.js的适用场景
查看>>
MongoDB 3.4 高可用集群搭建(二)replica set 副本集
查看>>
一个一线城市的IT白领的生活成本:3万/年
查看>>
ubuntu12.04 使用Adobe Reader PDF
查看>>
吃货联盟订餐系统(二)
查看>>
MessageBox 用法
查看>>
Developing school contest 2
查看>>
本文来自CSDN博客 map
查看>>