Using SQLite in Flutter

In the newly created file, we need to create a singleton.Why we need singleton: We use the singleton pattern to ensure that we have only one class instance and provide a global point access to it1.Create a private constructor that can be used only inside the class :2.Setup the databaseNext we will create the database object and provide it with a getter where we will instantiate the database if it’s not (lazy initialization).If there is no object assigned to the database, we use the initDB function to create the database. In this function, we will get the path for storing the database and create the desired tables:NOTE: The database name is TestDB and the only table we have is called Client. If you don't know what's going on you really need to go and learn some SQL it's more important than water.3. Create the Model ClassThe data inside your database will be converted into Dart Maps so first, we need to create the model classes with toMap and fromMap methods. I am not going to cover how to do this manually. If you don’t know how to do this, you should consider reading this article by Poojã Bhaumik.To create our model classes, I am going to use this website. If you don’t already have it bookmarked, you really should :)You can click here to see how it all worksOur Model:4..CRUD operationsCreateThe SQFlite package provides two ways to handle these operations using RawSQL queries or by using table name and a map which contains the data :Using rawInsert :Using insert :Another example using the biggest ID as a new ID:ReadGet Client by idIn the above code, we provide the query with an id as the argument using whereArgs..We then return the first result if the list is not empty else we return null.Get all Clients with a conditionIn this example I used rawQuery and I mapped the result list to a list of Client objects:Example: Only get the Blocked ClientsUpdateUpdate an existing ClientExample: Block or unblock a Client:DeleteDelete one ClientDelete All ClientsDemoFor our demo, we will create a simple Flutter app to interact with our database.We will first start with the app’s layout:Notes :1..The FutureBuilder is used to get the data from the database.2..The FAB to adds a random client to the database when it’s clicked.3..A CircularProgressIndicator is shown if there is no data.4..When the user clicks the checkbox the client will be blocked or unblocked according to the current state.Now it’s very easy to add new features, for example, if you want to delete a client when the item is swiped, just wrap ListTile with a Dismissible Widget like this:For our OnDismissed function, we are using the Database provider to call the deleteClient method..For the argument, we are passing the item’s id.Refactoring to use BLoC PatternWe have done a lot in this article but in real world application, making state part of the UI isn’t really a good thing..Instead, we should always keep them separated.There are a lot of patterns for managing state in Flutter but I will use BLoC in this article because it’s very flexible.Create the BLoC :Notes :getClients will get the data from the Database (Client table) asynchronously..We will call this method whenever we update the table hence the reason for placing it into the constructor’s bodyWe StreamController<T>.broadcast constructor so that we are able to listen to the stream more than once..In our example, it doesn't make much of a difference since we are only listening to the stream once but it is good to consider cases where you want to listen to the stream more than once.Don't forget to close your stream..This prevents us from getting memory leaks.. More details

Leave a Reply