Lesson5 - Introduction to Shared Preferences and Databases.pptx
Lesson5 - Introduction to Shared Preferences and Databases.pptx
FLUTTER
• A simple key-value storage system used to store
small amounts of data persistently in a Flutter
app. It's useful for storing user settings, SHARED PREFERENCES
authentication tokens, or simple app
preferences.
HOW TO INSTALL SHARED PREFERENCES IN FLUTTER?
Method 1: Simply from the command line of your flutter project.
NOTE:
dart pub get - For Dart projects
flutter pub get - For Flutter projects
Step 1: Visit the flutter site https://pub.dev/packages/shared_preferences/install to check the latest version of shared preferences:
Step 3: Go to the pubspec.yaml in your flutter project to input the plugin. Under dependencies add a line like this to your package's
pubspec.yaml.
Step 4: If the automatic implicit execution doesn’t happen then run an implicit flutter/dart pub get:
dart pub get - For Dart projects
flutter pub get - For Flutter projects
Step 5: To use the shared preferences to your dart code Import this:
import 'package:shared_preferences/shared_preferences.dart';
HERE ARE SMALL EXAMPLES THAT SHOW YOU HOW TO USE THE API (SHARED
PREFERENCES).
// Obtain shared preferences.
final SharedPreferences prefs = await SharedPreferences.getInstance();
// Try reading data from the 'counter' key. If it doesn't exist, returns null.
final int? counter = prefs.getInt('counter'); Note: This is the way to read
the data we previously wrote
// Try reading data from the 'repeat' key. If it doesn't exist, returns null. from the previous page. The
final bool? repeat = prefs.getBool('repeat'); key name used when writing
the data is the same key
// Try reading data from the 'decimal' key. If it doesn't exist, returns null. name you need to call to
final double? decimal = prefs.getDouble('decimal'); read or retrieve its value.
// Try reading data from the 'action' key. If it doesn't exist, returns null.
final String? action = prefs.getString('action');
// Try reading data from the 'items' key. If it doesn't exist, returns null.
final List<String>? items = prefs.getStringList('items');
OTHER COMMANDS USED IN SHARED PREFERENCES:
// Remove data for the 'counter' key.
await prefs.remove('counter');
prefs – is the instance of SharedPreferences or the object we used to call the methods or functions of SharedPreferences.
SharedPreferences prefs – this is how we declare the variable prefs na ang type is SharedPreferences.
await - Ensures that the getInstance() function completes before proceeding (since it is an asynchronous operation).
We used prefs.getString() to get the value, and since the datatype for
the ‘username’ key is a string. The prefs variable is a
SharedPreferences type. This is our instance to read data using the
.getString(‘key’) method.
Then the rest of this codes are how we build our UI for this
simple example of using shared preferences.
Schema Fixed schema (predefined structure) Flexible schema (dynamic and adaptable)
Scalability Vertically scalable (upgrading hardware) Horizontally scalable (adding more servers)
Data Integrity ACID-compliant (strong consistency) BASE-compliant (more available, less consistent)
Examples MySQL, PostgreSQL, Oracle, MS SQL Server MongoDB, Cassandra, CouchDB, Neo4j
WHAT IS SQL?
SQL databases, also known as Relational Database Management Systems (RDBMS), use structured tables to store
data. They rely on a predefined schema that determines the organization of data within tables, making them suitable for
applications that require a fixed, consistent structure.
•Structured Data: Data is organized in tables with rows and columns, making it easy to relate different types of
information.
•ACID Compliance: SQL databases follow the ACID properties (Atomicity, Consistency, Isolation, Durability) to ensure
reliable transactions and data integrity.
•Examples: Popular SQL databases include MySQL, PostgreSQL, Oracle, and MS SQL Server.
WHEN TO CHOOSE SQL?
SQL databases are well-suited for use cases where:
•Data consistency and transactional integrity are critical (e.g., banking systems, customer relationship management).
•Applications requiring multi-row transactions (such as inventory management) benefit from SQL’s robust features.
WHAT IS NOSQL?
NoSQL databases, on the other hand, are designed to handle unstructured or semi-structured data. Unlike SQL
databases, NoSQL offers dynamic schemas that allow for more flexible data storage, making them ideal for handling
massive volumes of data from various sources.
•Flexible Schema: NoSQL databases allow the storage of data without a predefined structure, making them more
adaptable to changing data requirements.
•CAP Theorem: NoSQL databases are designed based on the CAP theorem (Consistency, Availability, Partition Tolerance),
which prioritizes availability and partition tolerance over strict consistency.
•Examples: Well-known NoSQL databases include MongoDB, Cassandra, CouchDB, and HBase.
WHEN TO CHOOSE NOSQL?
NoSQL databases are a better choice when:
•You need to handle large, unstructured data sets, like social media data or logs.
•The application requires horizontal scalability to accommodate high traffic and big data.
•There is a need for real-time data processing and flexible data models (e.g., a content management system).
•You are dealing with applications requiring frequent changes in data structures.
SQL VS NOSQL: WHICH IS FASTER?
•SQL Databases: Generally, SQL databases perform well for complex queries, structured data, and systems
requiring data consistency and integrity. However, as the volume of data grows, they may struggle
with scalability and may require significant infrastructure upgrades.
•NoSQL Databases: NoSQL databases excel in scenarios that demand high performance and scalability. Because of
their horizontal scalability (accommodating more servers), they handle large amounts of data and high-velocity
workloads better. For instance, MongoDB or Cassandra is a common choice when dealing with big data or applications with
high traffic.
• SQLite is a great choice for local data storage in USING SQLITE FOR
Flutter when working with structured data. The
sqflite package provides a way to interact with
LOCAL DATA STORAGE
SQLite databases efficiently. IN FLUTTER.
USING SQLITE FOR LOCAL DATA STORAGE IN FLUTTER (WITH SQFLITE)
❖ At the file we will insert these line of codes (see notes for complete codes):
Retrieving Data
❖ Gets the database instance.
❖ Runs db.query('users') to fetch all users as a list of maps..
Updating Data
❖ Updates a user's name and age where id matches.
❖ whereArgs: [id]: Prevents SQL injection.
Deleting Data
❖ Deletes the user where id matches.
❖ whereArgs: [id]: Prevents SQL injection.
Third Step: Using SQLite in Flutter UI
❖ Now, let's create a simple UI to interact with the database.
❖ At the file we will insert these line of codes (see notes for complete codes):
Importing packages
❖ import 'package:flutter/material.dart’; - We need to import
this so that we can use material design components for
the User interface.
❖ import 'database_helper.dart’; - We need to import our
helper class for us to be able to interact with the SQLite
database we created from database_helper.dart file.
Deleting a User
❖ We need to Calls deleteUser(id) from
DatabaseHelper to remove the user by
using the users id.
❖ And then reloads the user list so that it will
update the users inside or list.
Building the UI
❖ Uses Scaffold to create a basic layout with an AppBar, TextField for name input and TextField for age input (we use numeric keyboard for
this), ElevatedButton for add or update users, Uses ListView.builder to display the list of users dynamically, and IconButton for our edit and
delete.
Last Step: Run you project application by using the command: flutter run
THANK YOU ☺
RYAN FERMO