How to use local storage in Flutter

Bo Bleyl
Flutter Community
Published in
3 min readJul 25, 2020

--

Sometimes there are just some things that need to be stored locally in your app. Dates, values, numbers, etc. Luckily, Flutter has a package (Shared_Preferences) that will handle this for both Android and iOS devices without any special configurations needed. It’s important to note that you should not store critical data locally. All important, critical data should be stored in a backend of your choosing. Shared_Preferences is meant to store simple data. Let's walk through an example.

Importing: https://pub.dev/packages/shared_preferences

dependencies:   
shared_preferences: ^0.5.8

This is the most recent version as of July 2020. Be sure to include the most recent version as found on the package installation page. Next, be sure to import the package into the file where you want to use local storage.

import 'package:shared_preferences/shared_preferences.dart';

In this example, we are going to be saving the user's display name when they log in, and each time that they re-open the app we will be displaying “Welcome back {displayName}”.

Reading data:

To start, we will want to make sure that we are checking to see if a value is already stored when the user first opens the app. To do this we want to call our initState method. Since…

--

--

Bo Bleyl
Flutter Community

Softare Developer @ Enterbridge. Father, Youtuber. Read More @ bleyl.dev