首页 > 解决方案 > Is there any way to write to a pre-existing text file in 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?

标签: flutter

解决方案


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);
 }
}

推荐阅读