Challenge
OK, here is the last challenge for you in this course.
There are two methods that retrieve several pieces of information about a file. They are:
file.stat();
file.statSync();
As you might guess, the difference between the two is that stat is asynchronous, and statSync is synchronous.
They return a FileStat object.
Here is the challenge for you: explore the FileStat object, then add an info button in the file screen that calls stat or statSync, and shows the information about the file in a dialog window. The end result should look like what you see on the screen.
OK, stop the video now, then start it again when you are ready. The solution is coming next!
Solution
Right, here is a possible solution to this challenge: in order to show the dialog with the file statistics, I have created an asynchronous method, called retrieveFileInfo.
Inside the method, first we check whether there is a filename or not, and if not we just return, without any further action.
As this is an async method, we can await the result of the getFile method in our FileHelper instance. Once we get the file, we can call the stat method.
As I’ve mentioned before, this returns a FileStat object. Let’s have a look at its properties: as you can see you have a changed, modified, accessed, type, mode and size properties. So, this is the information we’ll show our users.
The showDialog method shows a dialog to the app users, and takes a context, and a builder.
In the build method of the builder, we return an AlertDialog, with a title: in this case a text with “FIle information”, and a builder. For the content of the alert dialog, we can just insert a column, aligning the cross axis, which is the horizontal axis in this case with start (meaning it will align everything to the left if you use a language where you write from left to right).
For the main axis, which is the vertical axis in this case, we align the items with the spaceBetween value, meaning that all children of the column will have the same space between them, and take all the available space.
And the children are just text widgets, with the properties of the FileStat object, so the path, changed, that’s the creation date, modified, accessed, the mode, size and type. By the way, mode contains a sequence of digits that gives information about the permissions of the file.
At the bottom of the dialog, we also add an OK textButton, that pops the dialog and returns to the file screen.
OK, the last step for this challenge is calling this method to display the dialog. So in the AppBar we can add another IconButton, with an info icon, that when pressed calls the retrieveFileInfo method.
If you try this out, when you click on one of the files, and you get to the file page, if you press the info icon you see the dialog box with all the file information that you’ve retrieved.
So this ends this challenge. There are probably several other ways to achieve the same, so if you found another way and it works for you, that’s probably great as well.
OK, let’s recap what you’ve learnt in this course next!