SQLite supports all the relational database operations such as store, manipulate and retrieving data from database. It is a C-language library and is serverless and lightweight solution. To store data in device’s local storage, Android provides built in open source SQL database which stores data in it. In this article we will learn how to use SQLite in flutter apps and save data locally in flutter apps. So, we load data from server once and save data in local storage and use that to perform next actions. This is helpful when we want to give offline access to our users and don’t want to fetch data again and again from server. String path = join(documentsDirectory.path, "ProductDB.As you know almost in every app, storing data on local storage is an essential functionality. Create product table and load initial data during the creation of the database itself.ĭirectory documentsDirectory = await getApplicationDocumentsDirectory() SQLiteDBProvoider object and its method can be accessed through the static db variable.Ĭreate a method to get database (Future option) of type Future. Static final SQLiteDbProvider db = SQLiteDbProvider._() Sqflite is used to manipulate SQLite database.ĭeclare a singleton based, static SQLiteDbProvider object as specified below − Path_provider is used to get temporary and application path. Path is used to access dart core utility function related to file paths. Io is used to access files and directories. Import 'package:path_provider/path_provider.dart' Īsync is used to write asynchronous methods. Import necessary import statement in Database.dart. Product(this.id, this.name, scription, this.price, this.image) Ĭreate a new file, Database.dart in the lib folder to write SQLite related functionality. fromMap and toMap are used to serialize and de- serialize the Product object and it is used in database manipulation methods. Also, add a new method, toMap to convert product object into Map object. In database, we need primary key, id as additional field along with Product properties like name, price, etc., So, add id property in the Product class. Android studio will get the package from Internet and properly configure it for the application. Use the latest version number of sqflite in place of any.Īndroid studio will alert that the pubspec.yaml is updated.Ĭlick Get dependencies option. Here, path_provider package is used to get temporary folder path of the system and path of the application. Use the latest version number of sqflite in place of anyĬonfigure path_provider package in the pubspec.yaml file as shown below − Replace the default startup code (main.dart) with our product_rest_app code.Ĭopy the assets folder from product_nav_app to product_rest_app and add assets inside the *pubspec.yaml` file.Ĭonfigure sqflite package in the pubspec.yaml file as shown below − Let us create a product application to store and fetch product information from a standard SQLite database engine using sqflite package and understand the concept behind the SQLite database and sqflite package.Ĭreate a new Flutter application in Android studio, product_sqlite_app. The core functionality provided by sqflite package is as follows −Ĭreate / Open (openDatabase method) a SQLite database.Įxecute SQL statement (execute method) against SQLite database.Īdvanced query methods (query method) to reduce to code required to query and get information from SQLite database. It provides standard methods to manipulate SQLite database engine. sqflite package provides a lot of functionality to work efficiently with SQLite database. It is small and time-tested database engine. SQLite database is the de-facto and standard SQL based embedded database engine. In this chapter, let us discuss each of them in detail. Sqflite − Used to access and manipulate SQLite database, andįirebase_database − Used to access and manipulate cloud hosted NoSQL database from Google. Flutter provides many advanced packages to work with databases.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |