When to use Darudb
Darudb brings a simple, file‑based persistence layer to Flutter applications. Powered by the DaruDB engine, the package offers a Dart‑friendly API that lets you create, read, update, and delete records without the overhead of a full‑blown server or complex ORM. All data lives in one portable file on the device, making backups, migrations, and debugging straightforward. Because the database runs entirely in‑process, you get fast, deterministic reads and writes that work offline and require no network permissions.
When to choose Darudb? It shines in scenarios where you need reliable local storage but don’t want to manage SQLite schemas or deal with platform‑specific quirks. Think of note‑taking apps, personal finance trackers, offline‑first task managers, or any prototype that benefits from a quick, zero‑configuration data store. It also serves as a convenient cache for remote APIs, allowing you to persist API responses and serve them instantly on subsequent launches.
In a typical Flutter architecture, Darudb sits at the data layer, behind repositories and use‑case classes. You can inject a Darudb instance into your repository via Provider, Riverpod, or GetIt, keeping the rest of the UI layer agnostic of the storage mechanism. Because the package works on Android, iOS, macOS, Windows, Linux, and Web (with limited support), it fits cleanly into multi‑platform projects. The API is asynchronous, returning Futures and Streams, which aligns well with Flutter’s reactive patterns and makes it easy to combine with state‑management solutions like Bloc or Riverpod.
Getting started is straightforward: add the dependency, initialize the database with a file path, and start using the CRUD helpers. For production apps, consider encrypting the file if you store sensitive data, and be mindful of file size limits on mobile platforms. Regularly compact the database to reclaim space, and test migration paths when you evolve the schema. Darudb also provides optional transaction support, allowing you to batch multiple operations atomically, which is crucial for maintaining data integrity during complex updates.
For beginners, Darudb offers a gentle learning curve. You can spin up a local database in a few lines of code, experiment with simple models, and see immediate results in the UI. The package includes thorough documentation, example projects, and a clear error‑handling strategy, making it an excellent stepping stone before moving to more heavyweight solutions like SQLite or Firestore. Whether you’re building a hobby project or a production‑grade app that needs reliable offline storage, Darudb delivers a balance of simplicity, performance, and cross‑platform consistency.
Pros
- zero configuration
- single file backup
- cross‑platform
- async API
- lightweight
Watch outs
- limited query language vs. SQL
- no built‑in cloud sync
- file size constraints on mobile
Setup notes
Add the dependency with: ``` flutter pub add darudb ``` Then import the package and initialize the database, typically in your main function or a dedicated service class. ```dart import 'package:darudb/darudb.dart'; Future<void> initDb() async { final db = await DaruDb.open('my_app_data.db'); // Register models, run migrations, etc. } ```
Darudb works on Android, iOS, macOS, Windows, Linux, and Web (read‑only mode on Web). Requires Dart 2.17+ and Flutter 3.0+. On mobile platforms ensure the app has file‑system permissions; on Web, the database is stored in IndexedDB and cannot be opened in write mode due to browser restrictions.
```dart
import 'package:flutter/material.dart';
import 'package:darudb/darudb.dart';
class Note {
final String id;
final String title;
final String content;
Note({required this.id, required this.title, required this.content});
Map<String, dynamic> toMap() => {'id': id, 'title': title, 'content': content};
static Note fromMap(Map<String, dynamic> map) => Note(
id: map['id'],
title: map['title'],
content: map['content'],
);
}
class NotesRepository {
final DaruDb _db;
NotesRepository(this._db);
Future<void> add(Note note) => _db.insert('notes', note.toMap());
Future<List<Note>> getAll() async {
final rows = await _db.query('notes');
return rows.map((r) => Note.fromMap(r)).toList();
}
Future<void> delete(String id) => _db.delete('notes', where: 'id = ?', whereArgs: [id]);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final db = await DaruDb.open('notes.db');
await db.execute('CREATE TABLE IF NOT EXISTS notes (id TEXT PRIMARY KEY, title TEXT, content TEXT)');
runApp(MyApp(NotesRepository(db)));
}
class MyApp extends StatelessWidget {
final NotesRepository repo;
const MyApp(this.repo, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Darudb Demo')),
body: FutureBuilder<List<Note>>(
future: repo.getAll(),
builder: (c, snap) {
if (!snap.hasData) return const CircularProgressIndicator();
final notes = snap.data!;
return ListView.builder(
itemCount: notes.length,
itemBuilder: (_, i) => ListTile(title: Text(notes[i].title)),
);
},
),
),
);
}
}
```