flutter-文件读取

1、本地文件

void readFileDemo(){
      _readFileContent().then((value){
          print("value:$value");
          _writeFileContent("value:$value 12345").then((value){
              _readFileContent().then((value){
                 print(value);
              });
          });
      });
  }

///本地文件相关
  Future _localFile()async{
      ///获取document的路径的文件
      final directory = await getApplicationDocumentsDirectory();
      final path = directory.path;
      return File('$path/file.txt');
  }

  //文件写入
  Future _writeFileContent(contents) async{
    final file = await _localFile();
    return file.writeAsString(contents);
  }

///文件读取
  Future _readFileContent()async{
    try{
      File file = await _localFile();
      String contents = await file.readAsString();//文件读取
        return contents;
    }catch(e){
      return "";
    }
  }

2、资源文件读取

import 'package:flutter/services.dart' show rootBundle;
Future loadAssest() async {
      return await rootBundle.loadString("");
  }

你可能感兴趣的:(flutter-文件读取)