supabase_flutter
flutter integration for supabase. this package makes it simple for developers to build secure and scalable products.
supabase is an open source firebase alternative. we are a service to:
- listen to database changes
- query your tables, including filtering, pagination, and deeply nested relationships (like graphql)
- create, update, and delete rows
- manage your users and their permissions
- interact with your database using a simple ui
status
- [x] alpha: under heavy development
- [x] public alpha: ready for testing. but go easy on us, there will be bugs and missing functionality.
- [x] public beta: stable. no breaking changes expected in this version but possible bugs.
- [ ] public: production-ready
getting started
import the package:
import 'package:supabase_flutter/supabase_flutter.dart';
intialize supabase
before using it:
import 'package:supabase_flutter/supabase_flutter.dart';
void main() {
widgetsflutterbinding.ensureinitialized();
supabase.initialize(
url: supabase_url,
anonkey: supabase_annon_key,
authcallbackurlhostname: 'login-callback', // optional
debug: true // optional
);
runapp(myapp());
}
authcallbackurlhostname
is optional. it will be used to filter supabase authentication redirect deeplink. you need to provide this param if you use deeplink for other features on the app.
debug
is optional. it’s enabled by default if you’re running the app in debug mode (flutter run --debug
).
authentication
using authentication can be done easily.
email authentication
import 'package:supabase_flutter/supabase_flutter.dart';
void signin(string email, string password) async {
final response = await supabase.instance.client.auth.signin(email: _email, password: _password);
if (reponse.error != null) {
/// handle error
} else {
/// sign in with success
}
}
supabaseauthstate
it helps you handle authentication with deeplink from 3rd party service like google, github, twitter…
for more details, take a look at the example here
when using with a nested authentication flow, remember to call
startauthobserver()
andstopauthobserver()
before/after navigation to new screen to prevent multiple observers running at the same time. take a look at the example here
supabaseauthrequiredstate
it helps you protect route that requires an authenticated user.
for more details, take a look at the example here
signinwithprovider
this method will automatically launch the auth url and open a browser for user to sign in with 3rd party login.
supabase.instance.client.auth.signinwithprovider(
provider.github,
options: supabase.authoptions(redirectto: ''),
);
custom localstorage
as default supabase_flutter
uses hive
plugin to persist user session. however you can use any other plugins by creating a localstorage
impl.
for example, we can use flutter_secure_storage
plugin to store the user session in a secure storage.
// define the custom localstorage implementation
class securelocalstorage extends localstorage {
securelocalstorage() : super(
initialize: () async {},
hasaccesstoken: () {
const storage = fluttersecurestorage();
return storage.containskey(key: supabasepersistsessionkey);
}, accesstoken: () {
const storage = fluttersecurestorage();
return storage.read(key: supabasepersistsessionkey);
}, removepersistedsession: () {
const storage = fluttersecurestorage();
return storage.delete(key: supabasepersistsessionkey);
}, persistsession: (string value) {
const storage = fluttersecurestorage();
return storage.write(key: supabasepersistsessionkey, value: value);
},
);
}
// use it when initializing
supabase.initialize(
...
localstorage: securelocalstorage(),
);
you can use emptylocalstorage
to disable session persistance:
supabase.initialize(
...
localstorage: const emptylocalstorage(),
);
deeplink config
supabase redirect urls config
- go to your supabase project authentication settings page.
- you need to enter your app redirect callback on
additional redirect urls
field.
the redirect callback url should have this format [your_scheme]://[your_auth_hostname]
supabase 3rd party logins config
follow the guide https://supabase.io/docs/guides/auth#third-party-logins
for android
deep links can have any custom scheme. the downside is that any app can claim a scheme, so make sure yours are as unique as possible, eg. hst0000001://host.com
.
<manifest ...>
<!-- ... other tags -->
<application ...>
<activity ...>
<!-- ... other tags -->
<!-- deep links -->
<intent-filter>
<action android:name="android.intent.action.view" />
<category android:name="android.intent.category.default" />
<category android:name="android.intent.category.browsable" />
<!-- accepts uris that begin with your_scheme://your_host -->
<data
android:scheme="[your_scheme]"
android:host="[your_host]" />
</intent-filter>
</activity>
</application>
</manifest>
the android:host
attribute is optional for deep links.
for more info: https://developer.android.com/training/app-links/deep-linking
for ios
custom url schemes can have… any custom scheme and there is no host specificity, nor entitlements or a hosted file. the downside is that any app can claim any scheme, so make sure yours is as unique as possible, eg. hst0000001
or myincrediblyawesomescheme
.
for custom url schemes you need to declare the scheme in
ios/runner/info.plist
(or through xcode’s target info editor,
under url types):
<!-- ... other tags -->
<plist>
<dict>
<!-- ... other tags -->
<key>cfbundleurltypes</key>
<array>
<dict>
<key>cfbundletyperole</key>
<string>editor</string>
<key>cfbundleurlschemes</key>
<array>
<string>[your_scheme]</string>
</array>
</dict>
</array>
<!-- ... other tags -->
</dict>
</plist>
this allows for your app to be started from your_scheme://anything
links.
for more info: https://developer.apple.com/documentation/xcode/defining-a-custom-url-scheme-for-your-app
Comments are closed.