0% found this document useful (0 votes)
24 views12 pages

App Development8

App_development8

Uploaded by

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

App Development8

App_development8

Uploaded by

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

Flutter Local Data Persistence

Sameeha moogab
2024
Outline
• Local data persistence
• SQLite
Introduction
• Local data persistence is the process of storing data
on a local device, such as a phone or tablet. This data
will remain on the device even after the app is closed
or the device is restarted. It is important for mobile
apps because it allows apps to store user data, such
as settings, login data, or game data.
How Server Database works
• The stored data can be accessed again by the app at
any time, even when the device is not connected to
the internet. Flutter offers various methods to
perform local data persistence, such as:
● Key-value storage
● File storage
● Database
Key-value storage
 Key-value storage is the simplest method of storing data. Data
is stored in the form of keyvalue pairs, where the key is a string
and the value can be any data type.
SQLite
• This makes it ideal for applications that require quick access to data,
such as mobile applications. SQLite is a relational database, which
means that data is stored in tables consisting of rows and columns.
• Each row represents one record, and each column represents one
attribute of that record. SQLite provides various standard RDBMS
features, such as:
o CREATE TABLE : Creates a new table.
o INSERT INTO : Adds new data to a table
o SELECT : Retrieves data from a table.
o UPDATE : Changes the data in the table.
o DELETE : Deletes data from a table.
sqflite package
 sqflite package is one of the flutter packages that provides an
interface to the SQLite database, a lightweight and widely used
database engine embedded in most mobile operating systems.
 Key concepts used in the sqflite package are as follows:
 Database : A collection of organized data stored on the device.
 Tables : Structures within a database that hold related data in rows
and columns.
 Columns : Represent a specific attribute of the data, having a name
and a data type.
 Rows : Individual records within a table, each containing values for
the table's columns.
sqflite package
The core functionality provided by sqflite package is as follows :
1. Import the package
import 'package:sqflite/sqflite.dart';

2. Open a database with database instance

// Get a database instance


Database db = await openDatabase('my_database.db');
sqflite package
• Execute SQL statement (execute method) against
SQLite database.
await db.execute(''' CREATE TABLE IF NOT EXISTS tasks
( id INTEGER PRIMARY KEY AUTOINCREMENT, title
TEXT, description TEXT, completed INTEGER ) ''');
sqflite package
await db.insert('tasks', { 'title': 'Task 1', 'description':
'Description of Task 1', 'completed': 0 });

• Query/Read data, usually will be stored at List<Map>


because the data we will get is data of type Map.
List<Map> tasks = await db.query('tasks’);
sqflite package
Update data, important parameters in the update
method are the table name, one of the data
requirements with map data type, where the data is
stored, and provide a query as the argument (the
primary key).
await db.update('tasks', {'completed': 1}, where: 'id = ?',
whereArgs: [id]);
sqflite package
• Delete/Remove data, important parameters in the delete
method are the table name, where the data is stored, and
provide a query as the argument (the primary key).
await db.delete('tasks', where: 'id = ?',
whereArgs: [id]);

• Close the database await db.close(); Usually sqflite


package use asynchronous operations await when working
and also handle potential errors with try-catch blocks.

You might also like