write to file flutter code example

Example 1: flutter path provider

dependencies:
  path_provider: ^1.6.27

Example 2: write and read to file in flutter

Future get _localFile async {
  final path = await _localPath;
  return File('$path/counter.txt');
}

Example 3: write and read to file in flutter

Future readCounter() async {
  try {
    final file = await _localFile;

    // Read the file.
    String contents = await file.readAsString();

    return int.parse(contents);
  } catch (e) {
    // If encountering an error, return 0.
    return 0;
  }
}

Example 4: write and read to file in flutter

Future get _localPath async {
  final directory = await getApplicationDocumentsDirectory();

  return directory.path;
}

Tags:

Misc Example