首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将变量保存到文件颤振中

将变量保存到文件颤振中
EN

Stack Overflow用户
提问于 2018-06-13 18:12:11
回答 1查看 1.4K关注 0票数 2

我一直在寻找一段代码来将指定的变量保存到文本文件中。理想情况下,这可以通过简单的按一个按钮.来实现。

这是我想要实现的代码,以便在每个列表查看项的极右上,我可以按一个图标按钮并执行上述代码。

如果任何人对如何实现这一目标有任何想法,我将感谢任何建议。

代码语言:javascript
运行
复制
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);
                },
              ),
            )
          ],
        )
      );
  }

谢谢您抽时间见我。

编辑:

代码语言:javascript
运行
复制
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,
    );
  }

第二版:

代码语言:javascript
运行
复制
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');  

  }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-13 18:16:23

您可以使用provider获取临时或应用程序文档目录,然后在那里创建一个文件。

代码语言:javascript
运行
复制
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'); 
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50843680

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档