Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to write to a pre-existing text file in flutter?

Tags:

flutter

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?

like image 716
Coder Avatar asked Aug 07 '18 01:08

Coder


People also ask

How do I write to a file in flutter?

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. }


1 Answers

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);
 }
}
like image 171
Sanjeev S Avatar answered Oct 02 '22 22:10

Sanjeev S