【发布时间】:2019-04-14 20:50:47
【问题描述】:
我想将文件保存在 Android/iOS 的本地存储中。我按照 Flutter 食谱保存文件,但没有奏效。到处都有使用 File 类的例子,但是当我使用它时它是未定义的。我正在使用 Dart 2.2.0 和 Flutter 1.2.1
我尝试了一些网站的示例代码 sn-ps。没有任何效果。我的 Dart 文件中未定义文件类 readAsString 和 writeAsString。
这是代码。在 DartPad 中检查过。我哪里错了?
//packages
import 'dart:io';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
//start
class File {
/// Directory Path
/// Local Directory Path
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
// For your reference print the AppDoc directory
print(directory.path);
return directory.path;
}
/// Reference for file Location
Future<File> get _localFile async {
final path = await _localPath;
final address = '$path/data.txt';
return File(address);
}
/// Presenting different Data as 1 String
String convertingtoString(String title, String author, String content) {
return '$title\n$author\n\n$content';
}
/// Write to file
/// Writing as String
Future<File> writeContent(String matter) async {
/// Get matter converted to string as matter
final file = await _localFile;
// Write the file
return file.writeAsString(matter);
}
/// Read from file
Future<String> readcontent() async {
try {
final file = await _localFile;
// Read the file
String contents = await file.readAsString();
return contents;
} catch (e) {
// If there is an error reading, return a default String
return 'Error, Couldn\'t read file';
}
}
}
这是我的 android flutter 项目中的代码。我在 VScode 中遇到类似 DartPad 的错误
【问题讨论】:
标签: dart