afribase 0.2.0
afribase: ^0.2.0 copied to clipboard
The official Dart/Flutter client for Afribase — an open-source backend-as-a-service platform.
Afribase Dart/Flutter SDK #
The official Dart/Flutter client for Afribase — an open-source backend-as-a-service platform.
Installation #
Add to your pubspec.yaml:
dependencies:
afribase: ^0.2.0
Then run:
dart pub get
# or
flutter pub get
Quick Start #
import 'package:afribase/afribase.dart';
final client = AfribaseClient('https://your-project.useafribase.app', 'your-anon-key');
Authentication #
// Sign up
final res = await client.auth.signUp(email: 'user@example.com', password: 'secret123');
// Sign in
final res = await client.auth.signInWithPassword(email: 'user@example.com', password: 'secret123');
// OAuth (returns redirect URL)
final url = await client.auth.signInWithOAuth(provider: 'google');
// OTP / Magic Link
await client.auth.signInWithOtp(email: 'user@example.com');
final res = await client.auth.verifyOtp(email: 'user@example.com', token: '123456');
// Sign out
await client.auth.signOut();
// Listen for auth changes
final unsubscribe = client.auth.onAuthStateChange((event, session) {
print('Auth event: $event');
});
Database (PostgREST Query Builder) #
// Select
final data = await client.from('posts').select('*').eq('published', true).execute();
// Insert
final data = await client.from('posts').insert({'title': 'Hello', 'body': 'World'}).execute();
// Update
final data = await client.from('posts').update({'title': 'Updated'}).eq('id', 1).execute();
// Delete
final data = await client.from('posts').delete().eq('id', 1).execute();
// Filters & modifiers
final data = await client.from('posts')
.select('*')
.gte('likes', 10)
.order('created_at', ascending: false)
.limit(20)
.execute();
// RPC (Postgres functions)
final result = await client.rpc('my_function', {'arg': 'value'}).execute();
Storage #
import 'dart:typed_data';
// Upload
await client.storage.from('avatars').upload('avatar.png', fileBytes);
// Download
final Uint8List data = await client.storage.from('avatars').download('avatar.png');
// Signed URL
final url = await client.storage.from('avatars').createSignedUrl('avatar.png', expiresIn: 3600);
// Public URL (with optional transforms)
final url = client.storage.from('avatars').getPublicUrl('avatar.png',
transform: TransformOptions(width: 200));
// List files
final files = await client.storage.from('avatars').list(path: 'folder/');
// Move / Copy
await client.storage.from('avatars').move('old.png', 'new.png');
await client.storage.from('avatars').copy('source.png', 'dest.png');
// Bucket management
final buckets = await client.storage.listBuckets();
await client.storage.createBucket('my-bucket', public: true);
Edge Functions #
final result = await client.functions.invoke('my-function', body: {'name': 'world'});
Realtime #
// Create a channel
final channel = client.channel('room1');
// Listen for broadcast messages
channel.onBroadcast('message', (payload) => print(payload));
// Listen for Postgres changes
channel.onPostgresChanges(
event: 'INSERT',
schema: 'public',
table: 'messages',
callback: (payload) => print(payload),
);
// Presence
channel.onPresenceSync((state) => print(state));
// Subscribe
channel.subscribe();
// Send broadcast
channel.sendBroadcast('message', {'text': 'Hello!'});
// Track presence
channel.track({'user': 'alice', 'status': 'online'});
// Cleanup
client.dispose();
Requirements #
- Dart SDK >= 3.0.0
- Flutter >= 3.10.0 (for Flutter projects)
Dependencies #
http— HTTP clientweb_socket_channel— WebSocket supportmeta— Annotations
License #
MIT