Home > Back-end >  How return bool from write on file
How return bool from write on file

Time:05-15

Am using this for write file on phone

Future<File> writeData(data) async {
  final file = await _localFile;
  return file.writeAsString(data);
}

how can i know if it write successfully on file ? like is there a way to return a bool value when i write on file to know it write successfully ?

CodePudding user response:

Failures in file writing will be errors or exceptions. You could catch them all and return false in that case and otherwise true.

Future<bool> writeData(data) async {
  try {
    final file = await _localFile;
    file.writeAsString(data);

    return true;
  catch (_) {
    return false;
  }
}

Personal opinion ahead:

It would be wiser to handle those errors properly instead of returning a boolean though.

  • Related