flutter-drift

madteacher/mad-agents-skills · updated Apr 8, 2026

$npx skills add https://github.com/madteacher/mad-agents-skills --skill flutter-drift
0 commentsdiscussion
summary

Comprehensive guide for using drift database library in Flutter applications.

skill.md

Flutter Drift

Comprehensive guide for using drift database library in Flutter applications.

Overview

Flutter Drift skill provides complete guidance for implementing persistent local storage in Flutter apps using the drift library. Drift is a reactive persistence library for Flutter built on SQLite, offering type-safe queries, auto-updating streams, schema migrations, and cross-platform support.

Quick Start

Add dependencies to pubspec.yaml:

dependencies:
  drift: ^2.30.0
  drift_flutter: ^0.2.8
  path_provider: ^2.1.5

dev_dependencies:
  drift_dev: ^2.30.0
  build_runner: ^2.10.4

Define database:

(tables: [TodoItems])
class AppDatabase extends _$AppDatabase {
  AppDatabase([QueryExecutor? e])
      : super(
          e ??
              driftDatabase(
                name: 'app_db',
                native: const DriftNativeOptions(
                  databaseDirectory: getApplicationSupportDirectory,
                ),
                web: DriftWebOptions(
                  sqlite3Wasm: Uri.parse('sqlite3.wasm'),
                  driftWorker: Uri.parse('drift_worker.js'),
                ),
              ),
        );

  
  int get schemaVersion => 1;
}

Run code generator:

dart run build_runner build

Reference Files

See detailed documentation for each topic:

Common Patterns

Reactive Todo List with StreamBuilder

class TodoList extends StatelessWidget {
  
  Widget build(BuildContext context) {
    final database = Provider.of<AppDatabase>(context);

    return StreamBuilder<List<TodoItem>>(
      stream: select(database.todoItems).watch(),
      builder: (context, snapshot) {
        final todos = snapshot.data ?? [];
        return ListView.builder(
          itemCount: todos.length,
          itemBuilder: (context, index) {
            final todo = todos[index];
            return ListTile(
              title: Text(todo.title),
              trailing: Checkbox(
                value: todo.isCompleted,
                onChanged: (value) {
                  database.update(database.todoItems).replace(
                    TodoItem(
                      id: todo.id,
                      title: todo.title,
                      isCompleted: value ?? false,
                    ),
                  );
                },
              ),
            );
          },
        );
      },
    );
  }
}

Add Item with Form

Future<void> showAddTodoDialog(BuildContext context) async {
  final controller = TextEditingController();
  final database = Provider.of<AppDatabase>(context);

  await showDialog(
    context: context,
    builder: (context) {
      return AlertDialog(
        title: const Text('Add Todo'),
        content: TextField(
          controller: controller,
          decoration: const InputDecoration(labelText: 'Title'),
        ),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context),
            child: const Text('Cancel'),
          ),
          TextButton(
            onPressed: () async {
              if (controller.text.isNotEmpty) {
                await database.into(database.todoItems).insert(
                  TodoItemsCompanion.insert(title: controller.text),
                );
                if (context.mounted) {
                  Navigator.pop(context);
                }
              }
            },
            child: const Text('Add'),
          ),
        ],
      );
    },
  );

  controller.dispose();
}

Provider Setup

final databaseProvider = Provider<AppDatabase>((ref) {
  final database = AppDatabase();
  ref.onDispose(database.close);
  return database;
});

Database Migration


MigrationStrategy get migration {
  return MigrationStrategy(
      onUpgrade: stepByStep(
        from1To2: (m, schema) async {
          await m.addColumn(schema.todoItems, schema.todoItems.dueDate);
        },
      ),
    );
}

Platform-Specific Setup

Mobile (Android/iOS/macOS/Windows/Linux)

Uses drift_flutter with getApplicationSupportDirectory.

Web

Place sqlite3.wasm and drift_worker.js in web/ folder.

Isolate Sharing

AppDatabase.defaults(): super(
  driftDatabase(
    name: 'app_db',
    native: DriftNativeOptions(
      shareAcrossIsolates: true,
    ),
  ),
);

Testing

Use in-memory database for tests:

AppDatabase createTestDatabase() {
  return AppDatabase(NativeDatabase.memory());
}

Best Practices

  1. Use drift_flutter for easy database setup across platforms
  2. StreamBuilder for reactive UI updates
  3. Provider/Riverpod for database access management
  4. Close database on app/widget dispose
  5. Use migrations when schema changes
  6. Index columns used in WHERE clauses
  7. Limit stream query size for performance
  8. Use transactions for multi-step operations
  9. Debounce user input for search/filter
  10. Handle loading/error states in UI

Troubleshooting

Build Fails

dart run build_runner clean
dart run build_runner build --delete-conflicting-outputs

Migration Errors

dart run drift_dev schema validate
dart run drift_dev make-migrations

Stream Not Updating

Ensure operations go through drift APIs, not raw SQLite.

Discussion

Product Hunt–style comments (not star reviews)
  • No comments yet — start the thread.
general reviews

Ratings

4.773 reviews
  • Pratham Ware· Dec 28, 2024

    I recommend flutter-drift for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Dhruvi Jain· Dec 24, 2024

    flutter-drift is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Nia Gonzalez· Dec 24, 2024

    flutter-drift fits our agent workflows well — practical, well scoped, and easy to wire into existing repos.

  • Ama Yang· Dec 24, 2024

    flutter-drift is among the better-maintained entries we tried; worth keeping pinned for repeat workflows.

  • Ava Sharma· Dec 24, 2024

    Useful defaults in flutter-drift — fewer surprises than typical one-off scripts, and it plays nicely with `npx skills` flows.

  • Jin Khan· Dec 16, 2024

    We added flutter-drift from the explainx registry; install was straightforward and the SKILL.md answered most questions upfront.

  • Valentina Mensah· Dec 16, 2024

    I recommend flutter-drift for anyone iterating fast on agent tooling; clear intent and a small, reviewable surface area.

  • Oshnikdeep· Nov 15, 2024

    Keeps context tight: flutter-drift is the kind of skill you can hand to a new teammate without a long onboarding doc.

  • Soo Gonzalez· Nov 15, 2024

    Registry listing for flutter-drift matched our evaluation — installs cleanly and behaves as described in the markdown.

  • Ama Martin· Nov 15, 2024

    Keeps context tight: flutter-drift is the kind of skill you can hand to a new teammate without a long onboarding doc.

showing 1-10 of 73

1 / 8