我一直在寻找一段代码来将指定的变量保存到文本文件中。理想情况下,这可以通过简单的按一个按钮.来实现。
这是我想要实现的代码,以便在每个列表查看项的极右上,我可以按一个图标按钮并执行上述代码。
如果任何人对如何实现这一目标有任何想法,我将感谢任何建议。
Widget _cryptoWidget() {
return new Container(
child: new Column(
children: <Widget>[
new Flexible(
child: new ListView.builder(
itemCount: _currencies.length,
itemBuilder: (BuildContext context, int index) {
final int i = index ~/ 2;
final Crypto currency = _currencies[i];
final MaterialColor color = _colors[i % _colors.length];
if (index.isOdd) {
return new Divider();
}
return _getListItemUi(currency, color);
},
),
)
],
)
);
}谢谢您抽时间见我。
编辑:
ListTile _getListItemUi(Crypto currency, MaterialColor color) {
return new ListTile(
leading: new Image.network("http://cryptoicons.co/32@2x/color/"+currency.symbol.toLowerCase()+"@2x.png"),
title: new Text(currency.name,
style: new TextStyle(fontWeight: FontWeight.bold)),
subtitle:
_getSubtitleText(currency.price_usd, currency.percent_change_1h),
isThreeLine: true,
);
}第二版:
Widget _getSaveValueButton() {
new IconButton(
icon: new Icon(Icons.add),
onPressed: () { Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
new File('$appDocPath/my_file.txt').writeAsStringSync('myVar: $_currencies');
}发布于 2018-06-13 18:16:23
您可以使用provider获取临时或应用程序文档目录,然后在那里创建一个文件。
String someVal = 'some value to write to the file';
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
new File('$appDocPath/my_file.txt').writeAsStringSync('myVar: $someVal'); https://stackoverflow.com/questions/50843680
复制相似问题