What does the 'flush' argument of File.writeAsBytes do in Dart.io?

324

Since I/O traditionally is very expensive, there are a number of different layers of buffers that exist between your code and the physical disk to try to batch/coalesce I/O operations for efficiency and to avoid making your code wait around for I/O operations to complete.

"Flushed to the file system" means that all buffered write operations have been written to the file system (which is an OS abstraction on top of the physical disk). For example, if you call File.writeAsBytes without flushing, something else that tries to read from that file might still see the old contents, even if the corresponding Future has completed. If you perform the write with flushing, then reads of that file should be guaranteed to see the new contents when the Future has completed.

Why would someone knowledgable choose to set it to true?

You would choose to explicitly flush if you want to know with certainty when data has been written.

Why would someone knowledgable choose to set it to false?

You might not want to flush every write because doing so would be slower. For example, if you're making many small writes, it'd be better to issue all of them without flushing and then possibly flush on the last write.

Note that this is a general operating system concept and isn't at all specific to Dart.

Share:
324
Nerdy Bunz
Author by

Nerdy Bunz

Hi. Coding and creating new things gets me excited like a fizzy drink, but sometimes I run into headaches, so I come here for help. But I make sure to help other people too, so I think it works out. Apparently my posts often contain language considered extraneous by the pool cleaning robots who dutifully scrub it away on their regular patrols. "Bzzzz! Bzzzz!!! Desaturate, desaturate!" Fortunately, it's ACCESS DENIED for any editing attempts on this lil' article. They'll have to re-route the encryptions... and good luck with that. Oooo! Butterfly!!!! Anyway... when I'm not on here, I like to spend time sniffing and paddling my way around the enchanted river-forest near my house... and knitting my very own apps.

Updated on January 02, 2023

Comments

  • Nerdy Bunz
    Nerdy Bunz over 1 year

    In the dart.io documentation for File.writeAsBytes

    It says of the flush argument:

    If the argument flush is set to true, the data written will be flushed to the file system before the returned future completes.

    But I don't know what "flushed to the file system" means.

    Why would someone knowledgable choose to set it to true?

    Why would someone knowledgable choose to set it to false?

  • SardorbekR
    SardorbekR over 2 years
    So flush doesn't clean the buffer before everything is written to the file? Because I thought that flush command will clean everything from buffer even though writing process is not completed yet.
  • jamesdlin
    jamesdlin over 2 years
    @SardorbekR What do you mean by "clean the buffer"? A flush will write the used portion of the buffer to the file system.
  • SardorbekR
    SardorbekR over 2 years
    Initially I thought that flush removes buffered operations) but now I know that it works differently