Flutter Stuff

How to Save or Get Data on Shared Preferences in Flutter

When building a mobile application, storing and retrieving data is an essential task. In Flutter, one of the most common ways to do this is by using Shared Preferences. In this blog post, we’ll explore how to save and retrieve data using Shared Preferences in Flutter.

Table of Contents

What are Shared Preferences?

Shared Preferences is a built-in package in Flutter that allows you to store and retrieve small amounts of data, such as text, integers, booleans, and more. This data is stored in a file on the device’s storage, making it accessible across different screens and apps.

How to Use Shared Preferences in Flutter

To use Shared Preferences in Flutter, you’ll need to follow these steps:

Step 1: Add the Shared Preferences Package

First, add the Shared Preferences package to your Flutter project. You can do this by running the following command in your terminal:

flutter pub add shared_preferences
Dart

Step 2: Initialize Shared Preferences

Next, initialize Shared Preferences in your app by adding the following code to your main function:

import 'package:shared_preferences/shared_preferences.dart';
Future main() async {
WidgetsFlutterBinding.ensureInitialized();
final prefs = await SharedPreferences.getInstance();
// Initialize your app here
}
Dart

Step 3: Save Data

To save data using Shared Preferences, use the `setString`, `setInt`, `setBool`, or `setDouble` methods, depending on the type of data you want to store. For example, to save a string:

final prefs = await SharedPreferences.getInstance();
prefs.setString('myKey', 'myValue');
Dart

Step 4: Retrieve Data

To retrieve data using Shared Preferences, use the `getString`, `getInt`, `getBool`, or `getDouble` methods, depending on the type of data you stored. For example, to retrieve a string:

final prefs = await SharedPreferences.getInstance();
final myValue = prefs.getString('myKey');
Dart

Example Code

Here’s an example code snippet that demonstrates how to save and retrieve data using Shared Preferences:

import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefsExample extends StatefulWidget {
@override
_SharedPrefsExampleState createState() => _SharedPrefsExampleState();
}
class _SharedPrefsExampleState extends State {
final _formKey = GlobalKey();
String _name;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Shared Preferences Example'),
),
body: Form(
key: _formKey,
child: Column(
children: [
TextFormField(
decoration: InputDecoration(
labelText: 'Name',
),
validator: (value) {
if (value.isEmpty) {
return 'Please enter your name';
}
return null;
},
onSaved: (value) => _name = value,
),
SizedBox(height: 20),
ElevatedButton(
child: Text('Save Data'),
onPressed: () async {
final prefs = await SharedPreferences.getInstance();
prefs.setString('name', _name);
},
),
SizedBox(height: 20),
ElevatedButton(
child: Text('Retrieve Data'),
onPressed: () async {
final prefs = await SharedPreferences.getInstance();
final name = prefs.getString('name');
print('Name: $name');
},
),
],
),
),
);
}
}
Dart


Conclusion

In this blog post, we’ve explored how to use Shared Preferences in Flutter to save and retrieve data. By following these steps, you can easily store and retrieve small amounts of data in your Flutter app.

Whether you’re building a simple todo list or a complex game, Shared Preferences provides a convenient way to store and retrieve data without having to worry about file storage or persistence.

Leave a Comment

Scroll to Top