supabase-dart
A Dart client for Supabase.
Supabase is an open source Firebase alternative. We are a service to:
supabase-dart
mirrors the design of supabase-js
. Find the documentation here.
import 'package:supabase/supabase.dart';
main() {
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
// Select from table `countries` ordering by `name`
final response = await client
.from('countries')
.select()
.order('name', ascending: true)
.execute();
}
import 'package:supabase/supabase.dart';
main() {
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
// Set up a listener to listen to changes in `countries` table
final subscription = await client
.from('countries')
.on(SupabaseEventTypes.all, (payload) {
// Do something when there is an update
})
.subscribe();
// remember to remove subscription when you're done
client.removeSubscription(subscription);
}
Stream
To receive relatime updates, you have to first enable Realtime on from your Supabase console. You can read more here on how to enable it.
import 'package:supabase/supabase.dart';
main() {
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
// Set up a listener to listen to changes in `countries` table
final subscription = await client
.from('countries')
.stream()
.order('name')
.limit(30)
.execute()
.listen(_handleCountriesStream);
// remember to remove subscription when you're done
subscription.cancel();
}
import 'package:supabase/supabase.dart';
main() {
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
// Sign up user with email and password
final response = await client
.auth
.signUp('email', 'password');
}
import 'package:supabase/supabase.dart';
main() {
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
// Create file `example.txt` and upload it in `public` bucket
final file = File('example.txt');
file.writeAsStringSync('File content');
final storageResponse = await client
.storage
.from('public')
.upload('example.txt', file);
}
Initialize a SupabaseClient
by passing your Supabase URL and Supabase KEY. The keys can be found in your supabase project in /setting/API
.
final client = SupabaseClient('supabaseUrl', 'supabaseKey');
The client
has a auth
attribute (of type GoTrueClient
) that you can use to authenticate your users using supabase.
Use the signUp
method, which returns a GotrueSessionResponse
.
If the error
attribute is null
, the request was successful and the method returns data
of type Session
.
// Sign up user with email and password
final response = await client.auth.signUp('email', 'password');
if (response.error != null) {
// Error
print('Error: ${response.error?.message}');
} else {
// Success
final session = response.data;
}
Use the signIn
method. It works similar to the signUp
method.
// Sign in user with email and password
final response = await client.auth.signIn(email: 'email', password: 'password');
if (response.error != null) {
// Error
print('Error: ${response.error?.message}');
} else {
// Success
final session = response.data;
}
Use the signOut
method, which returns a GotrueResponse
.
Also for the sign out check that error
is null
to know if the request was successful.
// Sign out user
final response = await client.auth.signOut();
if (response.error != null) {
// Error
print('Error: ${response.error?.message}');
} else {
// Success
}
Check out the Official Documentation to learn all the other available methods.
This repo is licenced under MIT.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。