How do you use SQLite in Flutter?

← Flutter answers

Add the sqflite and path packages, open the database with openDatabase, create tables in onCreate, then use insert and query for data.

import 'package:path/path.dart';
import 'package:sqflite/sqflite.dart';
 
Future<Database> openAppDb() async {
  final path = join(await getDatabasesPath(), 'app.db');
  return openDatabase(
    path,
    version: 1,
    onCreate: (db, version) => db.execute(
      'CREATE TABLE notes(id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL)',
    ),
  );
}
 
Future<void> demo() async {
  final db = await openAppDb();
  await db.insert('notes', {'title': 'Buy milk'});
  final rows = await db.query('notes', where: 'title LIKE ?', whereArgs: ['%milk%']);
  print(rows);   // [{id: 1, title: Buy milk}]
}
  • Always pass user input through whereArgs, never by string concatenation.
  • Raise version and handle onUpgrade when you change the schema.
  • sqflite targets Android, iOS and macOS. For desktop and tests use sqflite_common_ffi.
  • For type-safe queries and migrations, drift builds on SQLite with code generation.

Related questions

Start debugging smarter today

Try Bugsee for free — no commitments, no hidden fees.

Free sign upLive demoNo credit card required