I have a txt file that I need to write to. I know that I can't write to this using File IO, so is there any way to write to this file?
Write to a file To write a string to a file, use the writeAsString method: import 'dart:io'; void main() async { final filename = 'file. txt'; var file = await File(filename). writeAsString('some content'); // Do something with the file. }
If you want to open the file in append mode you have to pass the file mode in your writeAsString()
function else it will over write the file.
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:path_provider/path_provider.dart';
Future<File> writeCounter(int counter) async {
final file = await File('your_path.txt');
// Write the file
return file.writeAsString('$counter',mode: FileMode.append);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With