Skip to main content

sqflite 2.4.6 copy "sqflite: ^2.4.6" to clipboard
sqflite: ^2.4.6 copied to clipboard

Flutter plugin for SQLite, a self-contained, high-reliability, embedded, SQL database engine.

example/example.md

sqflite example #

A minimal note list app storing its data in a SQLite database. It runs on Android, iOS and macOS.

Add the dependency:

flutter pub add sqflite

lib/main.dart:

import 'package:flutter/material.dart';
import 'package:sqflite/sqflite.dart';

/// Opened once, shared by the whole app.
final Future<Database> database = openDatabase(
  'notes.db', // relative to getDatabasesPath()
  version: 1,
  onCreate: (db, version) => db.execute(
    'CREATE TABLE Note (id INTEGER PRIMARY KEY, content TEXT NOT NULL)',
  ),
);

Future<List<String>> getNotes() async {
  var db = await database;
  var rows = await db.query('Note', orderBy: 'id DESC');
  return rows.map((row) => row['content'] as String).toList();
}

Future<void> addNote(String content) async {
  var db = await database;
  await db.insert('Note', {'content': content});
}

void main() {
  runApp(const MaterialApp(home: NotesPage()));
}

class NotesPage extends StatefulWidget {
  const NotesPage({super.key});

  @override
  State<NotesPage> createState() => _NotesPageState();
}

class _NotesPageState extends State<NotesPage> {
  late Future<List<String>> _notes = getNotes();

  Future<void> _add() async {
    await addNote('Note ${DateTime.now()}');
    setState(() {
      _notes = getNotes();
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Notes')),
      body: FutureBuilder<List<String>>(
        future: _notes,
        builder: (context, snapshot) {
          var notes = snapshot.data;
          if (notes == null) {
            return const Center(child: CircularProgressIndicator());
          }
          return ListView(
            children: [for (var note in notes) ListTile(title: Text(note))],
          );
        },
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _add,
        child: const Icon(Icons.add),
      ),
    );
  }
}

Windows, Linux and web #

The same code runs on other platforms once the global databaseFactory is set at the start of main(), see one code base for mobile, desktop and web.

Testing #

flutter test cannot use the plugin; use sqflite_common_ffi as a dev dependency, see unit testing.

More #

  • notepad_sqflite: a complete notepad app working on iOS/Android/Windows/Linux/macOS.
  • The example/lib folder of this package is the test application of the plugin (flutter run it to run the plugin tests on a device).
5.58k
likes
160
points
3.2M
downloads

Documentation

API reference

Publisher

verified publishertekartik.com

Weekly Downloads

Flutter plugin for SQLite, a self-contained, high-reliability, embedded, SQL database engine.

Repository (GitHub)
View/report issues

Topics

#sql #database

Funding

Consider supporting this project:

github.com

License

BSD-2-Clause (license)

Dependencies

flutter, path, sqflite_android, sqflite_common, sqflite_darwin, sqflite_platform_interface

More

Packages that depend on sqflite

Packages that implement sqflite