Summary
Full Transcript
Flutter Mobile App Development – Lecture 06: Advanced SQLite with Drift in Flutter 🎓 Course: Flutter Mobile App Development 👨🏫 Instructor: Dr. Abbas Malik 🏛 University: Prince Sultan University – College of Computer & Information Sciences 💡 Perfect for beginners in Flutter or anyone looking to strengthen their UI/UX layout skills in mobile development! 🔹 Course GitHub Repository: 📂 All course materials, code examples, and projects: https://github.com/mgmalik/flutterappdev Welcome back to IS487! In this follow-up lecture, we take a deep dive into the Drift package, a powerful and modern reactive persistence library for Flutter. Building on our previous SQLite introduction, we now explore how Drift brings type safety, stream queries, and a fluent Dart API to local database management. 🔹 What You'll Learn: Part 1: Introduction to Drift • What makes Drift different from sqflite • Key Features: o Type Safety: No more raw SQL string parsing errors o Stream Queries: Widgets that automatically watch for database changes o Fluent Queries: Intuitive methods and classes for database operations o Type-Safe SQL: Built-in parser and analyzer for writing SQL o Migration Utilities: Robust tools for schema migrations Part 2: Setting Up Drift in Your Project • Adding drift and path dependencies • Understanding the documentation: drift.simonbinder.eu • Code generation setup with build_runner Part 3: Defining Tables with Drift • Creating the Task model using Drift's Table class • Understanding Column Types in Drift: Dart Type Drift Column SQL Type int integer INTEGER BigInt int64 INTEGER String text TEXT bool boolean INTEGER (1 or 0) double real REAL Uint8List blob BLOB DateTime datetime INTEGER or TEXT • Auto-incrementing IDs and column constraints • Text columns with length validation • Boolean columns for task status Part 4: Building the Database Service • Creating the DatabaseService class • Using the @DriftDatabase annotation • Implementing the schema version • Setting up LazyDatabase for efficient connection management • Web platform configuration with sqlite3.wasm and drift_worker.dart.js • Running build_runner for code generation Part 5: Advanced Database Relationships • Foreign Key Relationships: dart class Albums extends Table { late final id = integer().autoIncrement(); late final name = text(); late final artist = integer().references(Artists, #id); } • Referential integrity in local databases Part 6: CRUD Operations with Drift's Fluent API • CREATE: Inserting records with TaskCompanion dart Futureint insertTask(TaskCompanion taskObj) { return into(task).insert(taskObj); } • READ: Fetching all records dart FutureListTaskData getAllTasks() { return select(task).get(); } • READ with Conditions: Filtered queries dart FutureListTaskData getSelectedTasks() { return (select(task) ..where((t) = t.id.isSmallerOrEqualValue(5)) ).get(); } • UPDATE: Modifying existing records dart Futurebool updateTask(TaskData taskData) { return update(task).replace(taskData); } • DELETE: Removing records by condition dart Futurevoid deleteTask(int id) { return (delete(task) ..where((t) = t.id.equals(id)) ).go(); } Part 7: Table Configuration Options • Nullable columns • Default values • Validation checks • Unique constraints • Indexes for performance Part 8: Integration with UI and State Management • Reactive streams for real-time UI updates • Watching database changes with Stream queries • Combining Drift with Riverpod for reactive state 🔹 Live Coding Demo: • Converting the sqflite ToDo app to Drift • Step-by-step code generation • Testing reactive queries • Debugging common drift issues 🔹 Packages Used: yaml dependencies: drift: ^2.14.0 sqlite3_flutter_libs: ^0.5.0 path_provider: ^2.0.0 path: ^1.9.0 dev_dependencies: drift_dev: ^2.14.0 build_runner: ^2.4.0 🔹 Key Takeaways: • Master type-safe database operations in Flutter • Build reactive UIs that respond to data changes • Understand when to choose Drift over sqflite • Implement complex database relationships locally • Efficient database connection management 🔹 Resources & References: • Drift Documentation: https://drift.simonbinder.eu • SQLite Official Site: https://www.sqlite.org/index.html • Drift Dart API Tables Guide: https://drift.simonbinder.eu/dart_api/tables • sqflite Package: https://pub.dev/packages/sqflite 💡 Perfect for developers who want to: • Write type-safe database code in Flutter • Build reactive, offline-first applications • Move beyond raw SQL to a fluent Dart API • Implement professional-grade local persistence ________________________________________ #Flutter #Drift #SQLite #Database #MobileDevelopment #TypeSafety #ReactiveProgramming #CRUD #ToDoApp #FlutterTutorial #AppDevelopment #LocalDatabase #PersistentStorage #PSU #IS487 #CodeGeneration
