05-Module 5
05-Module 5
Mobile Programming
MODULE 5:
Data Persistence and Design Patterns
Module 5
4. Firebase – Real-Time-Database
5. Design Patterns
2
Store key-value data on disk
(Shared Preference)
3
Data Persistence
Store key-value data on disk (Shared Preference)
https://docs.flutter.dev/cookbook/persistence/key-value
4
Data Persistence
Store key-value data on disk (Shared Preference)
https://docs.flutter.dev/cookbook/persistence/key-value
5
Read and write files
6
Data Persistence
Read and write files
• In some cases, you need to read and write files to disk. For
example, you might need to persist data across app launches, or
download data from the internet and save it for later offline use.
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
7
Data Persistence
Read and write files - Find the correct local path
Temporary directory
A temporary directory (cache) that the system can clear at any time. On iOS,
this corresponds to the NSCachesDirectory. On Android, this is the value that
getCacheDir() returns.
Documents directory
A directory for the app to store files that only it can access. The system clears
the directory only when the app is deleted. On iOS, this corresponds to the
NSDocumentDirectory. On Android, this is the AppData directory.
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
8
Data Persistence
Read and write files - Find the correct local path
This example stores information in the documents directory. You can find the
path to the documents directory as follows:
import 'package:path_provider/path_provider.dart';
// ···
Future<String> get _localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
9
Data Persistence
Read and write files - Create a reference to the file location
Once you know where to store the file, create a reference to the file's full
location. You can use the File class from the dart:io library to achieve this.
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
10
Data Persistence
Read and write files - Write data to the file
Now that you have a File to work with, use it to read and write data. First,
write some data to the file. The counter is an integer, but is written to the file
as a string using the '$counter' syntax.
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
11
Data Persistence
Read and write files - Read data from the file
Now that you have some data on disk, you can read it. Once again, use the File
class.
Future<int> readCounter() async {
try {
final file = await _localFile;
return int.parse(contents);
} catch (e) {
// If encountering an error, return 0
return 0;
}
}
https://docs.flutter.dev/cookbook/persistence/reading-writing-files
12
Persist data with SQLite
13
Data Persistence
Persist data with SQLite
• Flutter apps can make use of the SQLite databases via the
sqflite plugin available on pub.dev.
• Steps:
• 1. Add the dependencies
• To work with SQLite databases, import the sqflite
and path packages.
• The sqflite package provides classes and functions
to interact with a SQLite database.
• The path package provides functions to define the
location for storing the database on disk.
14
Data Persistence
Persist data with SQLite
Make sure to import the packages in the file you'll be working in.
import 'dart:async';
import 'package:flutter/widgets.dart';
import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
15
Data Persistence
Persist data with SQLite
For this example, define a Dog class that contains three pieces of
data: A unique id, the name, and the age of each dog.
class Dog {
final int id;
final String name;
final int age;
const Dog({
required this.id,
required this.name,
required this.age,
});
} 16
Data Persistence
Persist data with SQLite
Next, create a table to store information about various Dogs. For this
example, create a table called dogs that defines the data that can be
stored. Each Dog contains an id, name, and age. Therefore, these are
represented as three columns in the dogs table.
18
Data Persistence
Persist data with SQLite
Now that you have a database with a table suitable for storing
information about various dogs, it's time to read and write data.
First, insert a Dog into the dogs table. This involves two steps:
19
Data Persistence
Persist data with SQLite
https://docs.flutter.dev/cookbook/persistence/sqlite
20
Data Persistence
Persist data with SQLite
For example:
In this case, both the username and email columns must be unique.
https://docs.flutter.dev/cookbook/persistence/sqlite
21
Data Persistence
Persist data with SQLite
Inserting Data:
When you attempt to insert a new record, SQLite checks for existing
records that violate the unique constraint. If a duplicate is found, SQLite will
raise an error.
You can handle this in Flutter using a try-catch block. For example:
try { await database.insert('users', userData, conflictAlgorithm:
ConflictAlgorithm.fail);}
catch (e) { // Handle the error (e.g., show a message to the user)
print('Duplicate entry: $e');}
https://docs.flutter.dev/cookbook/persistence/sqlite
22
Data Persistence
Persist data with SQLite
Conflict Resolution:
SQLite provides several options for how to handle conflicts when inserting
data.
24
Data Persistence
Firebase – Real-Time-Database
https://firebase.google.com/docs/database/flutter/start
25
Design Patterns
26
Design Patterns
Flutter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
27
Design Patterns
Flutter
MVC: Model-View-Controller
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
28
Design Patterns
Flutter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
29
Design Patterns
Flutter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
30
Design Patterns
Flutter Implementing MVC in Flutter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
31
Design Patterns
Flutter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
32
Design Patterns
Flutter
MVP: Model-View-Presenter
Presenter: Acts as the middle-man, retrieving data from the Model and
formatting it for the View.
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
33
Design Patterns
Flutter
MVP: Model-
View-Presenter
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
34
Design Patterns
Flutter
MVVM is a pattern that's particularly well-
suited for frameworks with rich data-binding
mechanisms, like Flutter.
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
35
Design Patterns
Flutter MVVM: Model-View-ViewModel: To-do-list app
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
36
Design Patterns
Flutter
MVVM: Model-View-
ViewModel: To-do-list
app
https://medium.com/@sparkleo/design-patterns-in-flutter-implementing-mvc-mvp-and-mvvm-2b2ff24ec077
37
Design Patterns
Flutter
Clean Architecture
https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1
38
Design Patterns
Flutter
Clean Architecture provides a way to structure applications
that separate the different components of an application
into modules, each with a well-defined purpose. The main
Clean Architecture idea behind Clean Architecture is to separate the application
into three main layers: the presentation layer,
the domain layer, and the data layer.
https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1
39
Design Patterns
Clean
Flutter
Architecture
1- Presentation Layer
Responsibility
The Presentation Layer is the outermost layer, responsible for presenting
information to the user and capturing user interactions. It includes all the
components related to the user interface (UI), such as widgets, screens, and
presenters/controllers (State Management).
Components
• Screens: Represent the feature screens.
• Widgets and UI Components: Represent the visual elements of the application.
• Manager/Controllers: Contain the presentation logic that interacts with the UI
components. They receive user input, communicate with the Use Cases in the
Domain Layer, and update the UI accordingly.
• The Manager/Controllers layer can incorporate any state management solution,
such as BloC, Riverpod, Provider, and others.
https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1
40
Design Patterns
Clean
Flutter
Architecture
2- Domain Layer
Responsibility
The Domain Layer, also known as the Business Logic or Use Case Layer, contains the
core business rules and logic of the application. It represents the heart of the
software system, encapsulating the essential functionality that is independent of
any particular framework.
Components
•Entities: Represent the fundamental business objects or concepts.
•Use Cases: Contain application-specific business rules and orchestrate the flow of
data between entities. They are responsible for executing specific actions or
operations.
•Business Rules and Logic (Repository): Core functionality that is crucial to the
application’s domain.
https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1
41
Design Patterns
Clean
Flutter
Architecture
3- Data Layer
Responsibility
The Data Layer is responsible for interacting with external data sources, such as
databases, network services, or repositories. It handles the storage and retrieval of
data.
Components
•Repositories or Gateways: Abstract interfaces that define how data is accessed and
stored.
•Data Models: Represent the structure of the data as it is stored in the external data
sources.
•Data Sources: Implementations of repositories that interact with databases, APIs,
or other external services.
https://medium.com/@yamen.abd98/clean-architecture-in-flutter-mvvm-bloc-dio-79b1615530e1
42
End of Module 5