 Everybody's Brian, welcome to the 17th. I better double check that 17. Yes, 17th tutorial with Flutter got a little frazzled there. My phone was kind of beeping in the background. Anyways, we're going to continue on down our Flutter tutorials and we're going to learn a little bit about IO. Now, when I say IO, I don't mean the moon IO. I mean input output and the first part of input output we're going to work with is called files and directories. Files and directories is a huge topic and we're going to kind of condense it down a little bit. So obviously we can't cover every single little use case but we're going to cover the vast majority of them. We can't cover everything because we will be here for 300 more videos. You can do just about anything with files. You can encrypt them, decrypt them, encode them, decode them, pump them across the networks, scramble them up, chop them up, dice them. I mean, whatever you want to do, I hate to sound like an infomercial but files are quite literally the bread and butter of most programs. So without further ado, let's go ahead and import DartConvert, Dart IO and let's work with this. So the first thing we want to do here is let's say we want to figure out how to list files and directories in a path here. So let's say void list. How if I actually did that correctly? And we first want to surround this with a try catch block. Now, why do we want to do a try catch block first? It's because working with the file system can be very error prone. You've ever worked with an older OS or a computer that's kind of flaky? You'll know that really quickly that sometimes files just do not play nice. So, whoops, equal new. All right, so what we've got here is we've got a directory object and we're just going to make a new directory object based off the path. Then we're going to say, now if you notice how we can say exist and exist sync, this is kind of the crux of the IO portion of Dart. IO is asynchronous. So what they've done is they put some helper functions in here that allow you to help you call this synchronously. So instead of doing exist and then messing around with a wait and then futures and all that, we're just going to do the sync and let them do all the heavy lifting for us. So if it exists then, we're going to get files into the entity and we want to list sync. Then we're going to just print out. So the first thing we want to do here is actually, let's get a listing here. So we'll see. We're just going to, I'm on a Linux system, so we're just going to grab the root path. Users, you might want to do that on your C drive. Let's just run this and see what happens here. You can see how we get a whole list of things. These are, this is everything that is on my root path here. If you know Linux, it's really nothing special on this virtual machine. All right, so whoops, I got to pause the video for one second here. Sorry about that, somebody was at my front door so I had to go see what it was all about. We're going to change this path here and it's just the path that I have the videos in. That way we're working with a very small section of my file system. So if I goof up, I'm not reinstalling my operating system here, although on Linux it's really hard to actually blow something up. But you can see how we actually have a different structure now and this is actually the movie that we're creating at this very point in time. So what we want to do is we want to learn how to write to a file. I'm going to say bull and this is just the way I do it. There's the problem with programming or I should say the added bonus with programming is there's literally billions of ways you could do this. I'm just doing it my way. If you don't like it, feel free to do it your way. But this is typically what I do when I'm writing a program. All right, so we're going to do the try catch block. We did screw something up up here. Now, if you're wondering why I'm doing just a print to string, it's because I want to know when something actually blows up. The last thing you want is a what's called a silent error, which means you have a catch and then you don't do anything with it because then you run your program and nothing happens and you're like, what the heck? So you want to know that there was actually an error and that's why I just printed out. All right, so we need to say file. So we're just saying we want the file object to be a new file based off the path that we're giving it and I probably should name this the path, but I'm just going to leave it as file. And then we want to say a random access file. We're going to call this RF equal new random access file. Actually, no, sorry, I screwed up. We want to get that off of our file reference. Sorry about that. And we're going to say open sync. Now you'll notice that there's open read open, which gives a future and then open right open sync. We're just going to work with open sync. There's different types of files. We're going to work with what's called a random access file, meaning we can randomly move around in that file and do whatever we want with it. I typically work with random access files because they're a lot more flexible, although they are a bit slower. Technology has grown to the point that the difference between a random access file and say a sequential file is very minimal. And then we want to actually give it a mode and the mode here is going to be the mode that we give the function or I'm sorry, the method. If you're wondering what the mode actually means, you can actually say file mode dot and see the different modes. There's a pen, there's read, there's write, write only and write only append. Append basically takes an existing file if it exists. If it doesn't exist, it creates it, but let's just say it exists and it just keeps adding to the end of it. Think of it like a line in the bank. It'll just keep adding to the end of it. Read will read from the file. Write will completely destroy the file and write it from scratch. Think of writing as you take your parent a bad report card, they wad it up into a ball, throw it in the trash and say bring me another. Write only means you can only write to the file and write only append is pretty self-explanatory. You can only append to the file. Why they didn't call it append only, I don't know, but all right, so those are the different modes and that's pretty standard of most operating systems regardless of what platform you're on. That's kind of like a file primer. You probably spent a week talking about that in your computer science class. All right, so what we want to do now is we want to write string a sync and we're going to just say data and then we want to flush sync and we want to close sync. So flush and close, those are real key words. Kind of ignore the little sync at the end here for now. Sync just means that we are asking Dart to do this synchronously. Focus on flush and close. What flush does is think of a file as well like a toilet. I hate to say it that way, but think of it like a toilet and you're just dumping stuff into it and when you're done, you want to flush that toilet and write it down to the disk. So flush literally tells the operating system hey, write it down to the disk. Close then closes the file. I am not 100% certain, but I think under the hood close may call flush for you. I know on some different languages it actually does. I'm not sure if it does in Dart. So I'm going to call the flush intentionally because that is what was out in the actual code they had as an example out here somewhere. I'd have to find it. Yeah, right here, writing file contents. I think they're doing this because their example is asynchronous and I'm doing it synchronously. So I'm not sure if I actually need to do the flush. I think I just need to close. All right, so now that we know we can write to a file we need to know how to read from a file and this is a little bit easier and we're going to say string and we're going to say read file. All right, so we just do our try catch block. Now for some of you while I'm typing this out may be reading the comments out on YouTube. There's a lot of folks saying, hey, you know, we don't like these tutorials. We want more Qt or QML or why are you using Dart and not Qt and QML? It's because I've done a lot of other tutorials in another language called C++ that uses a library or I should say a bunch of libraries called the Qt. I don't want to call it the Qt framework because it's a Qt library but it's called Qt and it is kind of lost my train of thought here. It runs on any platform. It's an amazing system. I still love Qt. I still love QML and I want to do more videos on that but right now Dart and Flutter really have my attention. So that's just kind of my focus and we're going to say read S string sync. Now what we're doing here is we're just saying create a new file with the file name which creates the file object. Now that we have the file object F in this case we're saying F read S string sync. I mean that's going to read the contents of that entire file into one giant string and then return a variable. We are then going to take that variable and return it from this method and what we're doing here is the default encoding which is UTF-8 which is an entirely different conversation. Encoding is how the actual information is written and read to and from the disk. In this case we're going to leave it as UTF-8. You can feel free to experiment and change your encoding but just understand that not all encodings are compatible. So we're just going to leave that as default. All right. So now we have the ability to write and the ability to read. Let's test this out. Let's actually just go here and we want to say we need to make another variable here. Let's call this txt file and we're going to just say just give it a very simple name and we're going to say if write file and we need to give it our text file and we need to say some sort of data that we're going to write to and we're going to say and then the mode we're going to say append and we're just going to play around with this for a little bit. So really what we're saying if this then so what's going on out of the hood assuming this works because we haven't actually tested the code we're going to write to our text file which is going to be on this path in my virtual machine and we're going to write this data this right here is called an escape character and this right here is called an escape sequence. Wow I misspelled world that's embarrassing. So what escape does is it escapes out of the character sequence and says I want to put a special character in here slash n stands for new return you can if you're in a Windows machine I think it's slash n slash r or slash r slash n like that actually it's the other way around I can already tell by looking at it's rn which does a hard return a new line that's how Windows does it but on Linux and Mac it's just a new line you can also put like a tab in there you can put special characters you can put just anything you want we're just doing this to break it up so there'll be a line we're doing that because we're in append mode and we want a new line every time we run this then we're going to print and we're just reading the contents of the text file back that we just wrote and we should see hello world down here when we run this and you see bang hello world so if we go out to our file system you see that now there is a txt.txt our test.txt and if we were to open this up you can see there is hello world in there now let's just move this off to the side run this again and you see there's two now there's three now there's four and you can see our text file saying hey it's changed we're going to reload it and that's append mode it's just going to keep going and going and going now we're going to switch this to write mode notice how we only have one hello world and if we go back to our text file reload it there's only one that's the difference between write append append is going to keep adding to it where writes going to completely blow it away and start from scratch I'm going to switch this back to append because I want to put that up and get of the way it is now let's say we want to be able to let me think about this for just a moment here let's say we want to be able to actually pump this out as Jason so let's do that wasn't going to do it but I'm like yeah why not people are going to ask how to do it so we're going to say before we do that I'm actually going to do something else list sorry for being so scattered brain I'm kind of deviating from my original plan here I'm trying to think in advance of what people are going to ask for and what they always ask for is okay it's great that you can do it in one giant blob how do you do it in a bunch of lines you know how do you read lines from a file so I'm just trying to limit the number of questions that I'm going to get off of this thing and what we can do here is we can just say very simply and then we can just say read line sync and you see how that is currently going to return a list of strings and it's that easy right there we could just read the lines so let's actually do that and let's comment this out actually let's not let's say see whenever I deviate from from the plan here things happen very badly but that's when we probably learned the most so let's let's try this txt file so we want to get the length of this let's run that and you can see how there's two there's three there's four there's five there's six so there's six lines in that that list object so that is how you would read the individual lines and I'm actually going to comment this that was embarrassing wow I really just misspelled all of that so what read lines does is it goes in and it looks for that escape sequence of slash on or slash are or both and it actually breaks those up into individual strings so it's taken that giant string and breaking it down into six individual strings all right now we're going to go ahead and we're going to read and write some json here so let's actually all right let's say pool to let's say json to file I'm having a hard time figuring out what I want to call this there we go and we'll say string file now what we're going to do here is a little bit different what we're going to say is map and then we want a string and an int we're just going to basically plagiarize what we did on our last tutorial here and I am getting hungry like just sitting here something my stomach just starts growling so we're going to put it in absence and we're going to say Brian but yeah I've had a couple of people say what is cute what is QML why are people getting upset that you're not working on it that's why I've just I've done like I don't remember how many I think like 150 170 some odd videos a lot I've done a lot of those videos and I've gotten pretty advanced in that realm so anytime I make something that's not cute or QML everybody kind of wonders what the heck's going on yeah I'm going to get back to it eventually I'm actually hoping to get back to it this winter all right so we're going to say string Jason that whoops string data well Jason dot encode and we're going to encode this map and then we are going to return our right file and we're going to say the filing our data and then our mode we want this to be file mode right reason being if we have a pen will have multiple Jason objects in there and it'll probably crash the program or it will just get garbage back or we won't get the latest object something bad will happen alright so we've got Jason to file actually let's call this right Jason there that's better now I'm happy with it now we're going to say void read Jason I fought Jason for a long long time I was really into XML I did not like Jason when it first came out XML has just been around forever but it's horribly inefficient alright string data equal and we want to actually read the file so we're just reusing the code that we wrote so rather than reinvent the wheel with the right file read file we're just reusing our functions here now we need to do a little bit of air handling here you notice how right Jason is just returning the value of right file now with this we can't do that so what we need to do is say if data is empty then we're going to say print no data and then we're just going to return out of this function and we should also get the error string from the actual right here the actual method alright now now that we know that we have data because at this point we should have data say map string int this is the structure of our map here structure of our map that really didn't make a whole lot of sense but I think you guys knew what I meant alright so we're going to say people equal Jason decode and we're just going to decode that data into an actual object and then from there we're going to say print we want to know what the people object contains just so that we can prove that we actually decoded this we're going to say people not for each and then we're going to say key value scroll this down a little bit so you guys can see what's going on and then we're just going to print these out alright so that should work let's actually put this down in our main function here and we need a new file name so we're going to put this into a different file here and then we are going to simply say if right Jason takes the Jason then what we want to do is there we go so when we run this assuming it's all going to work we'll get another whoops we'll get another file out here actually go to our directory here we'll get another file out here called Jason dot txt and it will contain our Jason object and then if it returns true in the right where is it right right here if that returns true then right Jason will return true then we'll actually read it back and then we will go through this object and print it out after we've decoded it from string so this may seem a little complex but this is actually a really good real-world example this is really what Jason is designed for is you would take an object encode it down to a string pump it across the network or internet or whatever and then your consumer as it's called the consumer application would take it read that string decode it back into an object and then work with it and whoo that scared me it actually did work the first time you see we have seven hello worlds and our people object contains Brian is 43 years old and Heather is 25 years old and sure enough there's our Jason file and there is our Jason object that got written down to disk whoo that was a mouthful and I'm glad we didn't have any major errors because I'm tired and I'm hungry so I'm going to go get some food but before I do that I wanted to thank you guys for watching and you can find the source code for this and all of the tutorials out on my website void realms dot com just click on github and then go out to all the tutorials I got the flutter tutorials here and then all the source code on top of that feel free to visit the void realms Facebook group we have 1700 plus and counting programmers all different languages I warn you right now most of those animals love C++ you may get a little flak if you post a flutter question but actually everybody's pretty decent in there so they'll probably help you and that's it thanks for watching