Saving variables to a file - Flutter
1,323
You can use https://pub.dartlang.org/packages/path_provider to get a temp or app docs directory and then just create a file there
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');
Author by
Jake
Work email: jake(at)squaredsoftware.co.uk Please support me by downloading my new iOS application, travelrecce!
Updated on December 05, 2022Comments
-
Jake over 1 year
I've been looking for a piece of code to save a specified variable to a text file. Ideally this would work by simply pressing a button.
This is the code I would like to implement this into, so that on the far right of each listview item I can press an icon button and execute the aforementioned code.
If anyone has any ideas on how to achieve this, I would be grateful for any advice.
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); }, ), ) ], ) ); }
Thank you for your time.
Edit:
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, ); }
Second Edit:
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'); }
-
Jake almost 6 yearsThanks for the quick reply, any idea of how to get this to execute on a button press? Thanks
-
Günter Zöchbauer almost 6 years
new FlatButton(onPressed: () { /* above code here */ }, child: const Text('press me'))
-
Jake almost 6 yearsBrilliant, one last question though. Where would I implement the button into the widget: _cryptoWidget that I've pasted above? Thanks
-
Günter Zöchbauer almost 6 yearsNot sure, probably inside
_getListItemUi()
but don't know how it liiks like. -
Günter Zöchbauer almost 6 yearsCan you please edit your question and add the code there. Code in comments is unrradable.
-
Günter Zöchbauer almost 6 yearsHard to tell. Perhaps
new Row(children: [_getSubtitleText(currency.price_usd, currency.percent_change_1h), _getSaveValueButton()]),
-
Jake almost 6 yearsCan you please check the second edit, would this work? Also, await just after appDocDir = is highlighted red as it's undefined and not marked with async - any fix ideas? Thank you
-
Richard Heap almost 6 yearsAdd the async keyword
onPressed: () async { D...
-
Jake almost 6 yearsHey Gunter, Richard. This is an old post but where can I find the location of the file made upon button press?
-
Günter Zöchbauer almost 6 years@Jake what is "the file"?
-
Jake almost 6 yearsWhen the user presses a button, the buttons specific name is stored to a .txt file. The file should be in my Path/ directory but I can’t find it
-
Günter Zöchbauer almost 6 years@Jake don't know why you expect it to be in "my Path/ directory". You need to specify the path when you create the file and then it will be created there. To get a valid path, you can use the
path_provider
package. -
Jake almost 6 yearsLooking at your original answer above, it looks like it’s saved to (‘$appDocPath/my_file.txt’) but I’ve got no clue from there
-
Günter Zöchbauer almost 6 years@Jake so you are using
appDocPath
? If you don't get an error the file has to be there. You can useprint(myFile.absolute.path)
to get the resulting path. You can also try to read after write and print the output to check the file was actually created. If this works then the problem can only be somewhere else. -
Jake almost 6 yearsI'm Using
String appDocPath = appDocDir.path;
if that's what you mean. Also withprint(myFile.absolute.path);
I'm getting an error thatmyFile
is an undefined name. -
Günter Zöchbauer almost 6 yearsThat means that
myFile
is not in scope where you runprint(myFile.absolute.path)
. Please create a new question with more code. Comments are cumbersome for this purpose. -
Jake almost 6 yearsOkay sure, what code should I include in the new question?
-
Günter Zöchbauer almost 6 yearsIdeally you would create a new Flutter project and add to
lib/main.dart
only the minimal code necessary to reproduce and then post the content of thislib/main.dart
. -
Jake over 5 yearsWhat should the new question be? I'm thinking something like:
myFile
not in scope when I runprint(myFile.absolute.path)
-
Günter Zöchbauer over 5 yearsAdd the code that allows to reproduce the problem, describe what the current behavior is and what the expected behavior is. That should do. The title is not that important and can be adjusted later.