Flutter CRUD Operation Example – In this article I will explain how to develop simple CRUD (Create Read Update Delete) operation in flutter SDK with real time database of Firebase or SQLite. Without database we cant store and retrieve any data from server. So in order to get the data, we need database for save and access from anywhere. Here i give two database one is google Firebase and another one is MySQL. Both database is powerful when compared SQLite Firebase is best database because its cloud based so performance is very speed and reliable.
In SQLite we can store data in offline mode, that’s why most of developers choose lightweight of SQLite database. If you are beginner in SQLite read this article to learn more about SQLite database. Just move from MySQL to SQLite. Most of students still knows only about MySQL, they have no idea in SQLite.
Create Project
Okay lets start, first create one project in Visual Studio Code IDE or Android Studio platform. I suggest VS code IDE because its light weight and basic configuration enough to execute the flutter application. Download VS Code IDE on your system. Suppose if you don’t know how to create new project in VS Code just saw my flutter tutorial beginners. Click to watch the video learn more about flutter.
![](http://vetbossel.in/wp-content/uploads/2020/03/sqlite-database.png)
SQLite mostly used for every platform like java also. In android studio many android mobile application developers are choose SQLite rather than other database. SQLite data stored in user mobile device so its very speed, protocols are accessed by server side language like API, PHP, Dart, Java, Angular and Node etc.
Add dependency in flutter
Before starting the flutter crud operation example project, we must declare and initiate the dependency files of SQLite and flutter library. To work with SQLite database, first import sqlite and path packages.
dependencies:
flutter:
sdk: flutter
sqflite:
path:
Open the database
Before accessing the database we must open the connections. Then only access and store data. For more details read the official documentation flutter SDK SQLite database.
// Open the database and store the reference.
final Future <Database> database = openDatabase(
// Set the path to the database. Note: Using the `join` function from the
// `path` package is best practice to ensure the path is correctly
// constructed for each platform.
join(await getDatabasesPath(), 'flutter_database.db'),
);
After opening the database, we can create and delete the data in our wish. But that is mandatory for every projects. Then you can work with flutter sdk SQLite real time database.
Create the Table
Now create one table for store the user information which is uploaded by client side. In this example i will create the one table for flutter_beginners (this is the table name). There are four columns are here stored into a database. First one is ID its integer and names are string type.
final Future<Database> database = openDatabase(
// Set the path to the database.
join(await getDatabasesPath(), 'flutter_database.db'),
// When the database is first created, create a table to store dogs.
onCreate: (db, version) {
// Run the CREATE TABLE statement on the database.
return db.execute(
"CREATE TABLE flutter_beginner(id INTEGER PRIMARY KEY, name TEXT, age INTEGER)",
);
},
// Set the version. This executes the onCreate function and provides a
// path to perform database upgrades and downgrades.
version: 1,
);
Insert Data into the Database
Here i have to manually insert some value in the database. Use insert() method to store the map into an flutter_beginner table.
// Update the Dog class to include a `toMap` method.
class Dog {
final int id;
final String name;
final int age;
Dog({this.id, this.name, this.age});
// Convert a Dog into a Map. The keys must correspond to the names of the
// columns in the database.
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
'age': age,
};
}
}
// Define a function that inserts dogs into the database
Future<void> insertDog(Dog dog) async {
// Get a reference to the database.
final Database db = await database;
// Insert the Dog into the correct table. You might also specify the
// `conflictAlgorithm` to use in case the same dog is inserted twice.
//
// In this case, replace any previous data.
await db.insert(
'dogs',
dog.toMap(),
conflictAlgorithm: ConflictAlgorithm.replace,
);
}
// Create a Dog and add it to the dogs table.
final fido = Dog(
id: 0,
name: 'vetri',
age: 25,
);
await insertDog(fido);
Retrieve Data into the Database – Flutter CRUD Operation
After insert the data we can process the work of retrieve the data from flutter_beginner table. Execute the retrieval query of mapping methods to list out the entire data which is stored from the flutter sdk table.
// A method that retrieves all the dogs from the dogs table.
Future<List<Dog>> dogs() async {
// Get a reference to the database.
final Database db = await database;
// Query the table for all The Dogs.
final List<Map<String, dynamic>> maps = await db.query('dogs');
// Convert the List<Map<String, dynamic> into a List<Dog>.
return List.generate(maps.length, (i) {
return Dog(
id: maps[i]['id'],
name: maps[i]['name'],
age: maps[i]['age'],
);
});
}
// Now, use the method above to retrieve all the dogs.
print(await dogs()); // Prints a list that include Fido.
Download Source Code – Flutter CRUD Operation
In the above I have explain only for insert and retrieve the data from database. Through the source code you can get the source code of Flutter database CRUD operation example in SQLite.