Flutter: Shared Preferences In 5 Minutes | Data Persistence

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
story non-trivial data can sometimes be important so in this video we're going to learn how you can use the shared preferences plug-in to store the amount of times who have started the app and a big thanks to Shannon Galloway that's been the official sponsor of this video you can check out my page on down in the description and before we begin let's use checkout Skillshare skill show is an online learning platform where you can find over 27,000 different classes ranging from design business and tech if you use my link down in the description you will find that you will get two months of reuse so be sure to check it out now the link is in the description so first off we're going to add our shared preferences plug-in you can see the readme but we're just going to be going to the installation as I'm going to explain most of it so we're going to use the scroll down a bit go into the dependencies and then use copy dependencies so now if we navigate our pub spec ml we can go to below our Cupertino icon so use import the package will use go to use to share preferences as the latest version is zero point five point two then we're still going to run packages get and we will have the latest package and now I just created a home page and we're going to have a empty string called have started three times and this is just going to be displayed in the scaffold so we're just going to import some things in the scaffold so the first thing we're going to have inside here is that we're going to have a body of a center and the center is just going to have the text of the house started three times and then we're just going to set the textile to a font size of 32 so it's a bit easier to see so the first method we're going to create is called get int from shared preferences and this is just going to get the started number from our storage and then we'll use to return 0 if it's null so we'll start off by instituting our preferences plug-in and we'll do that by using the final press and then using their weight keyword two shared preferences and then we're just going to call get instance so we're going to store the value inside a variable called started number then we're just going to call our preferences and get int and it's in the request a string so this string is going to be the key we are going to set I'll just simply set this to startup number and you can change this to whatever you want to use be sure that you're using that throughout the app and now as that method should return null if it doesn't find anything we will have a if to shake that so first off we'll use pass in the startup number inside our if and then we'll just simply check if that is equal to null and then if it's equal to null will use return zero and then if it's not equal to null we least return that startup number so our next method will simply be a reset counter method and this will simply do what it says it will set the counter in the shared preferences to zero so we'll simply start by instantiating our shared preferences and then we'll simply call the Preferences of set int and if we check that method it requires a key and that key will be the startup number again and then it requires a value and we will simply set that value to zero so now we need some way of setting the counter and we'll simply do that with our in it state so now in our in state we can simply call our new method we're going to create and this method will simply be called increment startup so this method will simply increment the startup number and store it and then it will use set state to display that change in the UI so we're going to start by instantiating our preferences again and now we can simply create a new variable called last startup number and this variable will simply hold the value that comes from the last method we created which is used to get int from shared proof you can simply hover the mouth to see what the method does so below the last our top number we're going to instantiate a new variable called current startup number and this value will simply be the last startup number but incremented with one so before we do anything we're simply going to update a variable inside the shared preferences and we're going to use the key as before and then used to set that to the current start up number and just make sure it's the current start number and not the last our top number and now we can simply add some conditional shaking so for the first if we're used simply going to check the current start number and if it's equal to 3 we're going to display that in the UI so we'll start by using set state and then get the string that we had before and the string which is have started three times and then we're just simply going to assign that to a new string which is the started number times completed so now a optional thing we're going to do is use the reset the counter so if we have got until three again we is going to reset the counter so we can start over and we can simply do that we've used awaiting our reset counter and now for the L statement which is very easy we're just going to use set state again and then assigned have started three times to a new stream and now we should have our work implementation of shared preferences if you liked the video please let me know by liking subscribing to this channel and also Thank You Shannon Galway and Jeff Delaney for supporting me on patreon if you would like to check out that it's down in the description and I will see you in the next video bye
Info
Channel: Robert Brunhage
Views: 74,904
Rating: undefined out of 5
Keywords: Flutter, flutter persistence data, Flutter database, flutter sharedpreferences, flutter shared_preferences, flutter sharedpreferences example, Flutter data persitence, Flutter key, Flutter value, Flutter key value, Flutter key value pair, Flutter key value database, Flutter tutorial, Flutter basics, Flutter Basics tutorial, Flutter plugin, Flutter database tutorial, Flutter save data locally
Id: uyz0HrGUamc
Channel Id: undefined
Length: 5min 29sec (329 seconds)
Published: Mon Apr 29 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.