How to Save or Get Data on Shared Preferences in Flutter

In this example, we are going to show you how to save data like string, integer, boolean, string list, and double value to shared preference in Flutter. Shared preference is a easiest method to save data values in Flutter. 

See this also: How to use SQLite/Sqflite CRUD on Flutter App [Easiest Guide Example]

First, add shared_preferences plugin to your project by adding the following lines to pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  shared_preferences: ^2.0.15

Import the plugin to your script:

import 'package:shared_preferences/shared_preferences.dart';

SharedPreferences pre = await SharedPreferences.getInstance();
pre.setString("name", "FlutterCampus"); //save string
pre.setInt("age", 25); //save integer
pre.setBool("married", false); //save boolean
pre.setDouble("price", 240.50); //save double
pre.setStringList("tags", ["Flutter", "Dart", "App"]); //save List

SharedPreferences pre = await SharedPreferences.getInstance();
String name = pre.getString("name") ?? ""; 
//here "??" is a fallback operator, 
//if the return is null, it will be the assigned value;

int age = pre.getInt("age") ?? 0;
bool married = pre.getBool("married") ?? false;
double price = pre.getDouble("price") ?? 0.00;
List<String> tags = pre.getStringList("tags") ?? [];

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> { 
  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          body: Container(
            padding: EdgeInsets.only(top:80, left:20, right:20),
            child: Wrap(
                alignment: WrapAlignment.start,
                children: [
                  
                   ElevatedButton(
                    onPressed: () async {
                        SharedPreferences pre = await SharedPreferences.getInstance();
                        pre.setString("name", "FlutterCampus"); //save string
                        pre.setInt("age", 25); //save integer
                        pre.setBool("married", false); //save boolean
                        pre.setDouble("price", 240.50); //save double
                        pre.setStringList("tags", ["Flutter", "Dart", "App"]); //save List
                    }, 
                    child: Text("Save Data")
                  ),


                  ElevatedButton(
                    onPressed: () async {
                       SharedPreferences pre = await SharedPreferences.getInstance();
                       String name = pre.getString("name") ?? ""; 
                       //here "??" is a fallback operator, 
                       //if the return is null, it will be the assigned value;

                       int age = pre.getInt("age") ?? 0;
                       bool married = pre.getBool("married") ?? false;
                       double price = pre.getDouble("price") ?? 0.00;
                       List<String> tags = pre.getStringList("tags") ?? [];
                    }, 
                    child: Text("Get Data")
                  )
            ],)
          )
       );
  }
}

In this example, there is two elevated button, one is for saving data and another is for retrieving data from shared preference. You can use this methods to save and get data from shared preferences according to your need. 

In this way, you can save or retrieve data from shared preferences in Flutter. 

No any Comments on this Article


Please Wait...