initialize static method
- required String url,
- String? publishableKey,
- @Deprecated('Use publishableKey instead. anonKey will be removed in a future major version.') String? anonKey,
- Map<
String, String> ? headers, - Client? httpClient,
- RealtimeClientOptions realtimeClientOptions = const RealtimeClientOptions(),
- PostgrestClientOptions postgrestOptions = const PostgrestClientOptions(),
- StorageClientOptions storageOptions = const StorageClientOptions(),
- FlutterAuthClientOptions authOptions = const FlutterAuthClientOptions(),
- Future<
String?> accessToken()?, - bool? debug,
Initialize the current supabase instance
This must be called only once. If called more than once, an AssertionError is thrown
url and publishableKey can be found on your Supabase dashboard.
Use the publishable (anon) key here — never the secret key in a
Flutter app.
You can access none public schema by passing different schema.
Default headers can be overridden by specifying headers.
Pass localStorage to override the default local storage option used to
persist auth.
Custom http client can be used by passing httpClient parameter.
storageRetryAttempts specifies how many retry attempts there should be
to upload a file to Supabase storage when failed due to network
interruption.
Set authFlowType to AuthFlowType.implicit to use the old implicit flow for authentication
involving deep links.
PKCE flow uses shared preferences for storing the code verifier by default.
Pass a custom storage to pkceAsyncStorage to override the behavior.
If debug is set to true, debug logs will be printed in debug console. Default is kDebugMode.
Implementation
static Future<Supabase> initialize({
required String url,
String? publishableKey,
@Deprecated(
'Use publishableKey instead. anonKey will be removed in a future major version.',
)
String? anonKey,
Map<String, String>? headers,
Client? httpClient,
RealtimeClientOptions realtimeClientOptions = const RealtimeClientOptions(),
PostgrestClientOptions postgrestOptions = const PostgrestClientOptions(),
StorageClientOptions storageOptions = const StorageClientOptions(),
FlutterAuthClientOptions authOptions = const FlutterAuthClientOptions(),
Future<String?> Function()? accessToken,
bool? debug,
}) async {
assert(
publishableKey != null || anonKey != null,
'Either publishableKey or anonKey must be provided.',
);
final effectiveKey = publishableKey ?? anonKey!;
if (_instance._isInitialized) {
_log.info('Supabase is already initialized. Skipping reinitialization.');
return _instance;
}
_instance._debugEnable = debug ?? kDebugMode;
if (_instance._debugEnable) {
_instance._logSubscription = Logger('supabase').onRecord.listen((record) {
if (record.level >= Level.INFO) {
debugPrint(
'${record.loggerName}: ${record.level.name}: ${record.message} ${record.error ?? ""}',
);
}
});
}
_log.config("Initialize Supabase v$version");
if (authOptions.pkceAsyncStorage == null) {
authOptions = authOptions.copyWith(
pkceAsyncStorage: SharedPreferencesGotrueAsyncStorage(),
);
}
if (authOptions.localStorage == null) {
authOptions = authOptions.copyWith(
localStorage: SharedPreferencesLocalStorage(
persistSessionKey:
"sb-${Uri.parse(url).host.split(".").first}-auth-token",
),
);
}
_instance._init(
url,
effectiveKey,
httpClient: httpClient,
customHeaders: headers,
realtimeClientOptions: realtimeClientOptions,
authOptions: authOptions,
postgrestOptions: postgrestOptions,
storageOptions: storageOptions,
accessToken: accessToken,
);
if (accessToken == null) {
final supabaseAuth = SupabaseAuth();
_instance._supabaseAuth = supabaseAuth;
await supabaseAuth.initialize(options: authOptions);
// Wrap `recoverSession()` in a `CancelableOperation` so that it can be canceled in dispose
// if still in progress
_instance._restoreSessionCancellableOperation =
CancelableOperation.fromFuture(supabaseAuth.recoverSession());
}
_log.info('***** Supabase init completed *****');
return _instance;
}