zoukankan      html  css  js  c++  java
  • 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
    ', mode: FileMode.append);
      }
    
      Future<File> cleanFile() async {
        final file = await _localFile;
        bloc.dataBloc.add('clean');
        return file.writeAsString('');
      }
    }
    

      

  • 相关阅读:
    Java基础知识:正则表达式
    NodeJs 中 将表单数据转发到后台
    单片机的远程升级
    一些开源协议
    物联网的一些例子
    python一些开源特色库
    qt练习
    网页编程学习笔记
    PCB相关
    工业控制系统
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10686442.html
Copyright © 2011-2022 走看看