【发布时间】:2020-02-12 13:52:47
【问题描述】:
我有一个带有 TextField 的表单和一个能够保存和读取数据的提交按钮。
class Storage {
Future<String> get localPath async {
final dir = await getApplicationDocumentsDirectory();
return dir.path;
}
Future<File> get localFile async {
final path = await localPath;
return File('$path/db.txt');
}
Future<String> readData() async {
try {
final file = await localFile;
String body = await file.readAsString();
return body;
} catch (e) {
return e.toString();
}
}
Future<File> writeData(String data) async {
final file = await localFile;
return file.writeAsString('$data');
}
}
@override
void initState() {
super.initState();
widget.storage.readData().then((String value) {
setState(() {
name = value;
});
});
}
Future<File> writeData() async {
setState(() {
name = oneController.text;
oneController.text = '';
});
}
有了这个,我可以用字符串值保存数据。我尝试为 DateTime 做同样的事情,但我收到了这个错误:
"参数类型'Null Function(DateTime)'不能分配给参数类型'FutureOr Function(String)'"
保存到本地文件是否只适用于字符串数据?
【问题讨论】: