supabase-dart

Supabase的Dart客户端。

pub package
pub test


什么是Supabase?

Supabase是一个开源的Firebase替代品。我们是一个提供

  • 监听数据库更改
  • 查询您的表,包括过滤、分页和深度嵌套的关系(如 GraphQL)
  • 创建、更新和删除行
  • 管理您的用户及其权限
  • 通过简单的 UI 与数据库进行交互

状态

  • Alpha:开发中
  • 公开 Alpha:可供测试。但请对我们宽容些,会有 bug 和缺失的功能。
  • 公开 Beta:稳定。此版本预计不会有破坏性更改,但可能存在 bug。
  • 公开:生产就绪

文档

supabase-dart镜像了supabase-js的设计。文档请在此处查找:此处

使用示例

数据库

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

要接收实时更新,您需要先在Supabase控制台中启用实时功能。您可以在此处阅读有关如何启用它的更多信息:此处

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);
}

认证

通过传递您的Supabase URLSupabase KEY来初始化一个SupabaseClient。可以在您的supabase项目中的/setting/API找到这些密钥。

final client = SupabaseClient('supabaseUrl', 'supabaseKey');

client有一个auth属性(类型为GoTrueClient),您可以使用它通过supabase对用户进行身份验证。

注册

使用signUp方法,它返回一个GotrueSessionResponse

如果error属性为null,则表示请求成功,该方法返回类型为Sessiondata

// 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;
}

登录

使用signIn方法。它的工作方式与signUp方法类似。

// 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;
}

登出

使用signOut方法,它返回一个GotrueResponse

同样,对于登出,请检查error是否为null以了解请求是否成功。

// Sign out user
final response = await client.auth.signOut();

if (response.error != null) {
  // Error
  print('Error: ${response.error?.message}');
} else {
  // Success
}

查看官方文档以了解所有其他可用方法。

指南

  • Flutter Supabase认证 – 博客

贡献

  • 在GitHub上Fork此仓库:GitHub
  • 将项目克隆到您自己的机器上
  • 将更改提交到您自己的分支
  • 将您的工作推送到您的Fork
  • 提交Pull request,以便我们可以审查您的更改并进行合并

许可证

此仓库采用MIT许可证。

鸣谢

GitHub

https://github.com/supabase/supabase-dart