An integration of a sql database that works for flutter projects which uses a general implementation of the package sqflite
.
It uses also two other packages to help provide the application string to the app's folder.
Here I have used a custom Object called Water, from one of my projects to demonstrate how to use such an object with the database.
More on the dependencies in dependencies
- Download the DatabaseHelper.dart file;
- Place it in your project's lib folder;
- Add
WidgetsFlutterBinding.ensureInitialized();
inside the main method of your app; - Call an instance of it wherever you want to use it.
Water w = new Water(
waterDrunk: 1000,
dailyWater: 2500,
date: DateTime.now().toString()
);
// The id will be autogenerated
// Adds the object into a specific table in the database
DatabaseHelper.instance.AddWater(w);
It's possible to also create a general method that takes an argument for the table you want to put the object in.
// Using the object inserted in the code above
List<Water> list = await DatabaseHelper.instance.GetTable();
for(var v in list) print(v.waterDrunk); // 1000
Note how the await keyword is required because we are calling an external tool that comunicates with a database: this is telling the compiler to wait for a response from the database itself.
int id = 0;
Water w = new Water(
id: id,
waterDrunk: 1500,
dailyWater: 2500,
date: DateTime.now().toString
);
DatabaseHelper.instance.UpdateWater(w, id); // New value for waterDrunk = 1500;
int id = 0;
DatabaseHelper.instance.RemoveWater(id); // Removes the object with id = 0;
DatabaseHelper.instance.DeleteTable("water");
This project uses some external packages to work. External packages used:
Install them from pub.dev or just use:
$ flutter pub add sqflite
$ flutter pub add path_provider