• flutter 读写文件


    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
    ', 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 + '
    ';
        });
     
        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,
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      }
    }
    

      

    记得家权限.

  • 相关阅读:
    Hibernate使用固定值关联表
    使用spring-data-JPA调用存储过程
    angularjs动态添加节点时,绑定到$scope中
    JPA子查询
    表格表头固定的一种实现方式
    Unicode、UTF8与UTF16
    primefaces4.0基本教程以及增删改查
    tomcat发布webservice
    KonBoot – 只要5K映象文件轻易绕过您的WindowsXP/VISTA/7系统的密码
    Setting .xap MIME Type for Silverlight
  • 原文地址:https://www.cnblogs.com/pythonClub/p/10685892.html
Copyright © 2020-2023  润新知