APIs and SDKs
Dart and Flutter SDK
The official rustabase_dart_sdk package mirrors the JavaScript client and runs in Flutter on mobile, desktop, and web, and in plain Dart servers and tools.
Install
flutter pub add rustabase_dart_sdk
# or, for a plain Dart project
dart pub add rustabase_dart_sdkCreate a client and sign in
import "package:rustabase_dart_sdk/rustabase.dart";
final rb = createClient("https://my-app.rustabase.net");
await rb.auth("users").signInWithPassword("[email protected]", "a-strong-password");
print(rb.session.isValid);
print(rb.session.record?["id"]);Read and write records
final page = await rb.from("posts").list(
options: ListOptions(
filter: rb.filter("published = {:published}", {"published": true}),
sort: "-created",
expand: "author",
),
);
final all = await rb.from("posts").all();
final one = await rb.from("posts").get("RECORD_ID");
final latest = await rb.from("posts").first("featured = true");
final created = await rb.from("posts").create({"title": "Shipping the new API"});
await rb.from("posts").update(created["id"].toString(), {"published": true});
await rb.from("posts").remove(created["id"].toString());rb.filter fills {:name} placeholders safely, so user input can never change the meaning of a filter.
Keep users signed in
The default session lives in memory. In Flutter, back it with any storage package so users stay signed in between launches.
final session = AsyncSession(
initial: await storage.read("rb_session"),
save: (value) => storage.write("rb_session", value),
clearStorage: () => storage.remove("rb_session"),
);
final rb = createClient("https://my-app.rustabase.net", session: session);
rb.session.onChange.listen((_) => setState(() {}));
rb.signOut(); // clears the session and cancels pending requestsOther sign-in methods
final users = rb.auth("users");
// one-time code by email
final otp = await users.requestOtp("[email protected]");
await users.signInWithOtp(otp["otpId"]!, "123456");
// OAuth: opens the provider page, then finishes sign-in
await users.signInWithOAuth(provider: "google", openUrl: (url) => launchUrl(url));
await users.refresh();Realtime in a widget
late final Future<void> Function() stop;
@override
void initState() {
super.initState();
rb.from("posts").subscribe("*", (e) {
debugPrint("${e.action}: ${e.record}");
}).then((fn) => stop = fn);
}
@override
void dispose() {
stop();
super.dispose();
}Files, batch writes, and errors
import "package:http/http.dart" as http;
await rb.from("posts").create(
{"title": "Launch notes"},
files: [await http.MultipartFile.fromPath("cover", "cover.png")],
);
final url = rb.files.url(record, record["cover"].toString(), thumb: "300x200");
final batch = rb.batch();
batch.from("orders").create({"status": "new"});
batch.from("order_items").create({"sku": "A-1", "quantity": 2});
await batch.send();
try {
await rb.from("posts").get("missing-id");
} on RustaBaseError catch (e) {
print("${e.status} ${e.fieldErrors}");
}Never ship a superuser sign-in or an API key inside a mobile app. The app should only hold a user session.
Found something wrong on this page?
Fix it yourself. The link below opens this file in GitHub's editor and forks the repository for you if you need one, and your change becomes a pull request without leaving the browser.