This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')!, | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:fleet/fleet.dart'; | |
import 'package:flutter/material.dart'; | |
void main() { | |
runApp(const App()); | |
} | |
class App extends StatelessWidget { | |
const App({super.key}); |