Skip to content

Instantly share code, notes, and snippets.

View blaugold's full-sized avatar

Gabriel Terwesten blaugold

View GitHub Profile
dependencies:
cbl: ^1.0.0
cbl_flutter: ^1.0.0
# This dependency selects the Community Edition of Couchbase Lite.
# For the Enterprise Edition add `cbl_flutter_ee` instead.
cbl_flutter_ce: ^1.0.0
dev_dependencies:
# This dependency allows you to use Couchbase Lite in Flutter
# unit tests and not just in integration tests.
cbl_dart: ^1.0.0
import 'package:cbl_flutter/cbl_flutter.dart';
Future<void> main() async {
// If you're initializing Couchbase Lite in your `main` function
// make sure to initialize Flutter before Couchbase Lite.
WidgetsFlutterBinding.ensureInitialized();
// Now initialize Couchbase Lite.
await CouchbaseLiteFlutter.init();
import 'dart:io';
import 'package:cbl/cbl.dart';
import 'package:cbl_dart/cbl_dart.dart';
import 'package:flutter_test/flutter_test.dart';
void main() {
// Each test file needs to initialize Couchbase Lite.
// It's a good idea to encapsulate that in a util function,
// that all tests use.
// For this example, we'll put the database into a global variable.
late final AsyncDatabase database;
Future<void> openDatabase() async {
// The database name will be used as the part of the file name
// of the database.
database = await Database.openAsync('notes-app');
}
Future<MutableDocument> createNote({
required String title,
required String body,
}) async {
// In Couchbase Lite, data is stored in JSON like documents. The default
// constructor of MutableDocument creates a new document with a randomly
// generated id.
final doc = MutableDocument({
// Since documents of different types are all stored in the same database,
// it is customary to store the type of the document in the `type` field.
Future<void> createNoteFtsIndex() async {
// Existing documents will be indexed when an index is created.
await database.createIndex(
// Any existing index, with the same name, will be replaced with
// a new index, with the new configuration.
'note-fts',
FullTextIndexConfiguration(
// We want both the title and body of the note to be indexed.
['title', 'body'],
// By selecting the language, that is primarily used in the
Future<List<NoteSearchResult>> searchNotes(Query queryString) async {
// Creating a query has some overhead and if a query is executed
// many times, it should be created once and reused. For simplicity
// we don't do that here.
final query = await Query.fromN1ql(
database,
r'''
SELECT META().id AS id, title
FROM _
WHERE type = 'note' AND match(note-fts, $query)
class NoteSearchResult {
NoteSearchResult({required this.id, required this.title});
/// This method creates a NoteSearchResult from a query result.
static NoteSearchResult fromResult(Result result) => NoteSearchResult(
// The Result type has typed getters, to extract values from a result.
id: result.string('id')!,
title: result.string('title')!,
);
import 'package:flutter/material.dart';
import 'dart:ui';
const Color darkBlue = Color.fromARGB(255, 18, 32, 47);
void main() {
runApp(MyApp());
}
class MyApp extends StatefulWidget {
import 'package:fleet/fleet.dart';
import 'package:flutter/material.dart';
void main() {
runApp(const App());
}
class App extends StatelessWidget {
const App({super.key});