0% found this document useful (0 votes)
14 views6 pages

Session 10

The lesson plan covers data storage in Flutter, focusing on SharedPreferences for lightweight key-value storage and SQLite for structured relational data. It explains the use of both methods, including examples of saving and retrieving data, as well as CRUD operations with SQLite. Additionally, it includes practical exercises for implementing these storage solutions in applications.

Uploaded by

darkmail6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Session 10

The lesson plan covers data storage in Flutter, focusing on SharedPreferences for lightweight key-value storage and SQLite for structured relational data. It explains the use of both methods, including examples of saving and retrieving data, as well as CRUD operations with SQLite. Additionally, it includes practical exercises for implementing these storage solutions in applications.

Uploaded by

darkmail6969
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Lesson Plan for SharedPreferences and

SQLite in Flutter

1. Introduction to Data Storage in Flutter


• Why Data Persistence?

o Data persistence allows your app to store information even after the app is closed.

o Flutter offers several methods for local storage, including SharedPreferences (for
small key-value pairs) and SQLite (for more structured, relational data).

• Types of Local Storage:

o SharedPreferences: Used for simple, lightweight key-value storage.

o SQLite (sqflite): A local database used to store structured data in a relational


database format.

2. SharedPreferences in Flutter
What is SharedPreferences?

• SharedPreferences is used to store small amounts of simple data (key-value pairs), such
as user settings, preferences, or state information.

• It is asynchronous and stores data in persistent storage that remains available even after
the app is closed and reopened.

Key Concepts:

• Data types that can be stored include int, double, String, bool, and List<String>.

• It’s commonly used for saving settings like user preferences (e.g., theme, language, etc.).

Example of Using SharedPreferences

1. Add the dependency:

In your pubspec.yaml, add the shared_preferences dependency.

2. Saving and Reading Data using SharedPreferences:


Explanation:

▪ The app saves the value entered by the user in SharedPreferences.


▪ When the app is launched, it retrieves and displays the saved value.
▪ We used SharedPreferences.getInstance() to access the SharedPreferences
and setString to save the value.

3. sqflite in Flutter
What is SQLite?
• SQLite is a relational database management system used to store large and structured
data.
• It supports SQL queries and can store complex data types (multiple tables, foreign key
relationships, etc.).
Why sqflite?
• sqflite is a Flutter plugin that allows us to use SQLite databases for local storage in Flutter
applications.
• It is suitable for apps that need to manage larger and more complex data, such as to-do
lists, product catalogs, etc.
Key Concepts:
• SQLite is more structured than SharedPreferences, supporting tables, columns, rows, and
foreign key relationships.
• CRUD operations: Create, Read, Update, Delete are performed using SQL queries.
Example of Using sqflite

Add the dependency:

In your pubspec.yaml, add the sqflite and path dependencies.

Implementing SQLite CRUD Operations:


Explanation:
▪ The database is created using openDatabase(), and a table users is created
for storing user data.
▪ CRUD operations (Insert, Fetch) are demonstrated:
▪ Insert: New user data is inserted into the users table.
▪ Fetch: All users are fetched and displayed in a list.

4. Comparison: SharedPreferences vs SQLite


• SharedPreferences:
o Simple, key-value storage.
o Best for storing small amounts of data like user preferences (e.g., theme mode).
• SQLite (sqflite):
o Structured relational database.
o Suitable for storing large, complex datasets, supporting SQL queries and CRUD
operations.

5. Practical Exercise
• SharedPreferences Exercise:
o Create a simple app that allows the user to toggle between light and dark themes.
Save the theme preference using SharedPreferences.
• sqflite Exercise:
o Create a simple to-do list app where users can add, edit, and delete tasks. Use
SQLite to store tasks persistently.

You might also like