Reading and Writing Files

13 minutes
Share the link to this page
In some cases, it can be handy to read and write files to disk. This can be used to persist data across app launches, or to download data from the internet and save it for later offline use. In order to save files to disk, you’ll need to combine the path_provider plugin with the dart:io library.


Hello, in this video, I'm going to show you how to read and write to local files. So this is extremely useful. So you can have data persistence. And the benefit of this is if you launch the application, do something, save data, close down the application launch. Again, you can, you know, assess to continue where you left off, it's great if you may be downloaded data from the internet could be an image, it could be just simple text. And you want to save it for you know, later offline usage.

It might be because you want offline functionality or just so the user can access some of the without having to redownload it all from a speed perspective. Okay, so to use it pretty simple, you want to first go to the pub sector, pub spec dot YAML file, and we need to add the path on the school provider package to get the latest version and just open up your web browser. Go to pub for slash packages can literally search for the package that you are looking for tap on the school provider and the latest version and you can choose Copy this is 0.50 plus one but it might be a slightly different version. When you do this do packages dot get and now we have to start using the provider function. So, what we are first going to do is have a few inputs of input dot async import package flutter foundation and finally we are going to import package lot is going to be the path Provide a provider dot file.

And there we go. Okay, so now what we going to do is create a class. And this class is going to the counter story, who's going to have a counter on our screen, which when pressed, will eat, but the actual number that it's on will persist between app launches. So first of all, we're gonna have a future method, which is basically going to get the local path. Because this is useful, because on Android, the path will be slightly different but using this code, it will be dynamic because of what platform you are using. We just need one more directory equals away.

Get application documents directory. Turn The directory now we have a future file, we are going to get to the local file and get a synchronous and final path equals away underscore no call path. So you just literally using this method and then getting the path was this got the path, it will, we'll be able to get the file, so we just do return four file and the location is dollar path for whatever the actual location is for slash, and then the name of the file. We're going to call it counter dot txt. We can have as many as you want, it's good to do this in essentially this. So it basically Create the file at the start the application.

So there are a little bit of no no extra no tip number setup dates you should be doing. So we're going to take a number method. And this is going to allow us to read the content is going to be an integer, but the data that you want to read is totally up to you, you can modify this code to suit your needs. So let's try it cuz just in case didn't work, no, the file doesn't mean you can't add the entire file equals await underscore local file. So this will get the following case where this gets the path location. So this call right here basically combined these two, a sub been separated just in case we just want to get the local path anywhere else.

Then we are going to read the file folder contents numeric numbers we are just going to string them we could pass it nature on the foil don't read us string return and now we are going to pause it. We're going to pause the content the need to do because we know this just going to be interior will we need a lot just in case something goes wrong on the cusp bla bla return zero. Okay, so now we're gonna have one last method. You probably guessed that this is going to take a file and this is going to write to the file itself. And think and here equals away in the school local file. To write to the folder, file dot write, screen.

For the reader fitness water screen, it's gonna be a string, I was going to dollar counter. And I need to demo stuff future. Apart from that, we're all good to go. So to do. So, in widget rolling here, what we are going to do is we're going to create a final tend to storage on the corner storage. And we are going to pass through the actual storage.

I'm going to have a classroom that's going to be Let me think about this. Do we need to do that? No, we actually don't do eco. Counter storage like so. Okay, so what roles Okay, so what we're going to do in here we're going to create a interpret int on this book. And then we will see know what I'm going to create the constructor for my homepage.

And in here, we are going to say can turn up then value we are going to set state so this day Onslow County. Okay, so next we are going to create a method to increment the counter. We'll be implementing all the boards around the fins to display this feature file on the score counter itself. Go counter plus plus this is going to increment by one but what you do to what you store is totally up to you next need to wipe the variable the string to the widget storage dot white counter Okay, so now what we are going to basically add here, that column in here, we're gonna add some children. So here I'm going to have a piece of text and the child is going to be in school counter. Nope, it's not meant to be a counter.

Yo yo we have them tell rate raised But a child and I'm gonna say this isn't going to call increment. The increment counter method never reloaded. Forget that. Measure plus we'll call on No. Where am I looking? Let me try one last week override this instead.

Let me do a hot restart. Okay, there we go. Okay, so it's already at 13. So if you do Okay, so we're at 24. Now, if I shut down the application from that 24 Clifton store, we can just literally go to it knock down Gone should be this one, that means running. Why?

Testing me on my device, have fun on the device, it could be an emulator issue potentially. Let's just rerun it. So we haven't deleted the APK or anything the the application Okay, so installing the APK Go is still on 24. And if you were to do that, again, he was still there. So we use that to read the data. And that's basically how you read and write data, if you have any questions about any of this, so you know, this is gonna be generic.

You might have a different file name, we didn't write in county silver can tell you, my wife, something slightly different for the most part, it's going to be basically the same. And this you know, getting it initially going insane. And again, instead of incrementing, you could store something else, instead of that you just have something else. But that's it. You have any questions, feel free to drop me a message and I will definitely assist you and I look forward to seeing you in the next video.

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.