Persisting data is very important for the users because they don't want to type inforamtion everytime or to wait for network to load the same data again.they want thier data to be saved so let's make that happent
in this article we will use SQLite as a database just because it's the most used database for mobile apps . and to integrate it in flutter we will use sqflite
plugin since it's keeping up with the latest flutter and dart developemt.
in your project go to pubspec.yaml
and under dependencies
add the latest version of sqflite
and path_provider
(use the right numbers from pub)
https://gist.github.com/fcd336abe8eaa7d758c1efde5e827bc1
NOTE: we use the path_provider package to get the commonly used location such as TemporaryDirectory and ApplicationDocumentsDirectory
Now in your project add a new file and let's call it Database.dart
but before we do anything else we want one databse instance all the time . and to do that we need to implement the singleton pattern whcih is very easy in dart with the help of dart Factory constructors.
Why we need singlteon :
We use the singlten pattern to ensure that we have only one class instanse and provide a global point accses to it
1.Create a private consterctor that can be used only inside the class :
https://gist.github.com/649e59d3d5137ce2b780d5a211179bd5
2.Setup database
and now we will create the database object and provide it with a getter where will setup the database if it's not (lazy initialization).
https://gist.github.com/d6f896bcb005f0a9cff2a3d5bfbb8e92
and in the initDB
we will get the path wherewe will store the database and then create the table if no database found in the spicfied path :
https://gist.github.com/50b3ff2bba0485ba16f4e00d603c05e9
NOTE: The database name is TestDB
and the only table we have is called Client
and if you don't know what's going on you relly need to go and learn some SQL it's important than water.
The data inside your database will be converted into Dart maps so first we need to create model classes with toMap
and fromMap
methods. i am not goiing to coved how to do this manulty if you want to understad how that works consider reading this aricle by pooja . for me i am just goign to use this app which should be in your bookmarks from now and so on :
https://gist.github.com/92ddfbdb7ca39dc3e4f810a576415e42
the SQFlite packge provide two ways to do all the operation using RawSQL queries or using the table name and map that contas the data :
using rawInsert
:
https://gist.github.com/9177866df22b0d3a06f963d377ff3009
using insert
:
https://gist.github.com/dd02276cc3b1b8ae7f1570cc68a361dd
another example : the id is the biggest ID+1
https://gist.github.com/e6611787dbb9d3aa80a48152c6155d9c
https://gist.github.com/5d1fe0e9ea30ae0823df7b83bf74f593
In the above code we provide the query with the id
as an argument using whereArgs
and we return
the first result if the list is not emty otherwise we return null.
in this examlpe i used rawQuery
and i mapped the result list to a list of Client
objects
https://gist.github.com/ee27409b24a56178d463b91a303c7edf
Example : get only Blocked Clients
https://gist.github.com/afc84d8f76ce749d61deb3100de4494b
https://gist.github.com/bf8f43ecf717782b8b5652400dc1f287
Example : block or unblock a Client:
https://gist.github.com/aa893d0b95166ff51fca2b443f74208e
https://gist.github.com/6b8ffc5587de888b43a8fa60448d0e52
https://gist.github.com/cece3175af6e8078effb3a89b69a9743
in this part we will create a simple flutter app to intercat with our database but first let's build the app layout:
https://gist.github.com/c60460cb2c07c150d14e9adef64c378c
Notes :
1.I used a FutureBuilder to get the data from the database
2.I used a FAB to add a random client to the database
https://gist.github.com/80bb827130a07ca3f55c85a6f5d9c8c3
3.I will show a CircularProgressIndicator
if there is no data
4.When the user clicks the checkbox the client will be blocked or unbloked accoring to the currunt state
Now it's very easy to add new futures for example i want to delete the client when the item is swiped. just wrap you ListTile
with a Dismissible
Widget like this: