supabase_addons 0.0.1 supabase_addons: ^0.0.1 copied to clipboard
A starting point for Dart libraries or applications.
Supabase is an open source Firebase alternative. It has support for auth, database and storage updates. The goal of this package is to make things easier when using supabase.
Get started #
Add the dependencies to your pubspec.yaml
file:
dependencies:
supabase: <latest-version>
supabase_addons: <latest-version>
Import both supabase
and supabase_addons
:
import 'package:supabase/supabase.dart';
import 'package:supabase_addons/supabase_addons.dart';
First of all, we need to init the addons:
SupabaseAddons.initialize(
client: SupabaseClient(SUPABASE_URL, SUPABASE_SECRET),
storagePath: './auth'
);
After eveything is used, you have to dispose the addons. Failure to do so can result in performance issues:
SupabaseAddons.dispose();
Auth Addons #
The auth addon is able to persist the user session into the device storage. It is backed by hive, a lightweight and blazing fast key-value database written in pure Dart
Analytics #
Analytics is a database-addon.
Analytics provides insight on app usage and user engagement. Its reports help you understand clearly how your users behave, which enables you to make informed decisions regarding app marketing and performance optimizations.
Get started with Analytics #
Create a table called analytics
on the database:
create table public.analytics (
name text not null,
params json,
user_id text,
timestamp text
);
Initialize the analytics addon:
SupabaseAnalyticsAddons.initialize();
Log an event #
After initialized, you can log the events using SupabaseAnalyticsAddons.logEvent
.
You can pass three arguments:
- required
name
, the event name. It can't have any spaces. If any, they are replaced by_
. params
, the event info. It's not required to have params, but it's good to have when rendering the graphics. The more info, the betteruserId
, the user id on the event. If not provided, the current session is used
SupabaseAnalyticsAddons.logEvent('goal_completion', params: {
'name': 'lever_puzzle',
});
Auth events #
When the user is signed in or signed up, the user_session
event is triggered automatically.
You can disable the automatic logging when initializing by passing
logUserSignIn: false
.
The user_session
has some useful information on the params field:
country_code
, the user country.os
, the user operating system. This is aString
representing the operating system or platform. This is powered by (Platform.operatingSystem
)
Crashlytics #
Crashlytics is a crash reporter that helps you track, prioritize, and fix stability issues that erode your app quality.
Get started with Crashlytics #
Create a table called crashlytics
on the database:
create table public.crashlytics (
exception text not null,
stackTraceElements json,
reason text,
fatal bool,
timestamp text
);
Initialize the crashlytics addon:
SupabaseCrashlyticsAddons.initialize();
Toggle Collection #
import 'package:flutter/foundation.dart' show kDebugMode;
if (kDebugMode) {
// Force disable collection while doing every day development.
// Temporarily toggle this to true if you want to test crash reporting in your app.
SupabaseCrashlyticsAddons.collectionEnabled = false;
} else {
// Handle Crashlytics enabled status when not in Debug,
// e.g. allow your users to opt-in to crash reporting.
}
Recording an error #
To record an error with custom information, you can wrap the code in a try/catch block:
import 'package:flutter/foundation.dart' show kDebugMode;
try {
// code that may throw an exception
} catch (error, stacktrace) {
SupabaseCrashlyticsAddons.recordError(
error,
stacktrace,
reason: 'The user is very dumb', // the reason goes here
fatal: false, // whether the error is fatal, such as an app crash
printDetails: kDebugMode, // whether the error should be printed to the console. Usually only on debug mode
);
}
Handling uncaught errors #
If you're using Flutter, you can catch and report all the errors from the framework by redefining FlutterError.onError
with the following function:
FlutterError.onError = (details) {
FlutterError.dumpErrorToConsole(details, forceReport: true);
SupabaseCrashlyticsAddons.recordError(
details.exceptionAsString(),
details.stack,
reason: details.context,
printDetails: false,
);
}
To catch any other exception that may happen in your program, wrap the dart program in a runZonedGuarded
:
void main() async {
runZonedGuarded<Future<void>>(() async {
// the rest of your code goes here
runApp(MyApp());
}, (error, stacktrace) {
SupabaseCrashlyticsAddons.recordError(error, stacktrace);
});
}
Features and bugs #
Please file feature requests and bugs at the issue tracker.