initialize static method

Future<Supabase> initialize({
  1. required String url,
  2. required String anonKey,
  3. Map<String, String>? headers,
  4. Client? httpClient,
  5. RealtimeClientOptions realtimeClientOptions = const RealtimeClientOptions(),
  6. PostgrestClientOptions postgrestOptions = const PostgrestClientOptions(),
  7. StorageClientOptions storageOptions = const StorageClientOptions(),
  8. FlutterAuthClientOptions authOptions = const FlutterAuthClientOptions(),
  9. bool? debug,
})

Initialize the current supabase instance

This must be called only once. If called more than once, an AssertionError is thrown

url and anonKey can be found on your Supabase dashboard.

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.

Implementation

static Future<Supabase> initialize({
  required String url,
  required String anonKey,
  Map<String, String>? headers,
  Client? httpClient,
  RealtimeClientOptions realtimeClientOptions = const RealtimeClientOptions(),
  PostgrestClientOptions postgrestOptions = const PostgrestClientOptions(),
  StorageClientOptions storageOptions = const StorageClientOptions(),
  FlutterAuthClientOptions authOptions = const FlutterAuthClientOptions(),
  bool? debug,
}) async {
  assert(
    !_instance._initialized,
    'This instance is already initialized',
  );
  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,
    anonKey,
    httpClient: httpClient,
    customHeaders: headers,
    realtimeClientOptions: realtimeClientOptions,
    authOptions: authOptions,
    postgrestOptions: postgrestOptions,
    storageOptions: storageOptions,
  );
  _instance._debugEnable = debug ?? kDebugMode;
  _instance.log('***** Supabase init completed $_instance');

  _instance._supabaseAuth = SupabaseAuth();
  await _instance._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(
    _instance._supabaseAuth.recoverSession(),
  );

  return _instance;
}