Flutter, Google’s popular cross-platform UI framework, has revolutionized mobile app development. However, for many applications, efficient data management is crucial. SQLite, a lightweight, serverless database, offers a robust solution. Flutter Drift bridges the gap between these two technologies, providing a seamless and intuitive way to integrate SQLite into your Flutter projects.
In this comprehensive guide, we will delve into the world of Flutter Drift. We will explore its key features, benefits, and best practices. Whether you’re a seasoned Flutter developer or just starting your journey, this guide will equip you with the knowledge to harness the power of Flutter Drift and build powerful, data-driven mobile applications.
Setting up the project
Add the following dependencies in pubspec.yaml file.
dependencies:
drift: ^2.9.0
sqlite3_flutter_libs: ^0.5.0
path_provider: ^2.0.0
path: ^1.8.3
dev_dependencies:
drift_dev: ^2.9.0
build_runner: ^2.4.5
drift
: This is the core package defining most APIssqlite3_flutter_libs
: Ships the latestsqlite3
version with your Android or iOS app. This is not required when you’re not using Flutter, but then you need to take care of includingsqlite3
yourself.path_provider
andpath
: Used to find a suitable location to store the database. Maintained by the Flutter and Dart teamdrift_dev
: This development-only dependency generates query code based on your tables. It will not be included in your final app.build_runner
: Common tool for code generation, maintained by the Dart team
Configuration of Flutter Drift database
Within your Flutter project’s root directory, establish a new Dart file named ‘database.dart’. Inside this file, construct a class called ‘MyDatabase’ that extends the ‘_$MyDatabase’ class generated by the Drift code generator.
class MyDatabase extends _$MyDatabase {
MyDatabase() : super();
}
Now create a function _openConnection which will create a database file in the defined location.
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return NativeDatabase.createInBackground(file);
});
}
The ‘LazyDatabase’ utility facilitates asynchronous determination of the appropriate file location. The database file, named ‘db.sqlite’, resides within the application’s designated database directory. Incorporate the ‘schemaVersion’ attribute within the ‘MyDatabase’ class.
@override
int get schemaVersion => 1;
To generate the database.g.dart
which contains the _$MyDatabase
superclass, run dart run build_runner build
on the command line.
The final code will be:
import 'dart:io';
import 'package:drift/drift.dart';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
part 'database.g.dart';
@DriftDatabase()
class AppDb extends _$AppDb {
AppDb() : super(_openConnection());
@override
int get schemaVersion => 1;
}
LazyDatabase _openConnection() {
return LazyDatabase(() async {
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
return NativeDatabase.createInBackground(file);
});
}
Perform CRUD operation
Here’s a Flutter Drift Database example by created a crud app in Flutter.
Let’s create an employee table by creating a file employee_table.dart
import 'package:drift/drift.dart';
class Employees extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text().nullable()();
TeColumn get post => text().nullable()();
IntColumn get salary => integer().nullable()();
}
Conclusion
Flutter Drift has emerged as a valuable tool for Flutter developers seeking efficient and reliable data storage solutions. By providing a seamless integration with SQLite, Drift simplifies database operations and enhances the overall performance of your mobile applications.
In this guide, we have covered the fundamental aspects of Flutter Drift, from setting up your project to performing complex database queries. By following the best practices and leveraging the powerful features of Drift, you can create robust and scalable mobile applications that meet the demands of modern users.
As Flutter and SQLite continue to evolve, Flutter Drift is likely to play an even more significant role in the future of mobile app development. By mastering this technology, you can position yourself as a skilled developer capable of building innovative and high-quality applications.
Wanna Level up Your Flutter game? Then check out our ebook The Complete Guide to Flutter Developement where we teach you how to build production grade cross platform apps from scratch.Do check it out to completely Master Flutter framework from basic to advanced level.