Shared Preferences In Flutter

Updated 27 April 2023

Save

Hi everyone, this blog is about Shared Preferences using dart.

Here we will make a flutter app, in this example, we will make a login app to save data and as a result it will manage login sessions for users.

Users can enter login details and login into App and will be redirected to the home screen. Next time when the user comes in, app session is managed and as a result, it will directly redirect to the home page.

If you will logout from the home page by pressing the logout button. Then the user will be logged out and again redirect to the login page and will restart the session.

You may also check our Flutter app development services page

So let’s make an easy example using the shared preferences.

Now, first of all, we need to know what is Shared Preferences

Shared Preferences is a plugin for flutter apps. It is a local database that we use to store small data for the app. As we know, we can store data in shared preference in key-value pair form.

How can we use Shared Preferences

First of all, we need to add package dependencies to pubspec.yaml file.

First dependency for shared preferences

Second dependency for showing status of session on login logout actions.

Now we have to create two files

main.dart file for the login screen and a homepage.dart file for the Homepage screen

main.dart Code :

In this, we did our login page code and save the status of the session using shared preference after pressing the login button and navigate to the homepage page because the session status will logged in.

homepage.dart Code

In this, we did code for the homepage.dart and also provided a logout button to update the session status using shared preferences.

Here are images of login screen and homepage

I hope this blog will help you to learn about Shared Preferences and you will be able to implement them.

Happy Learning ✍️

Reference Link:

http://www.codeplayon.com/2021/06/flutter-shared-preferences-example/

https://flutter.dev/docs/cookbook/persistence/key-value

author
. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home