Skip to content

Commit cc058e7

Browse files
authored
feat(supabase_flutter): Make Supabase.initialize() idempotent (#1194)
[Supabase] Make initialize() idempotent This PR introduces a minor update to the initialize() method to avoid throwing when called multiple times. Instead, it logs an informational message and returns the existing instance, making initialization idempotent and smoother for production use. - Replaced assert(!_instance.isInitialized) with a runtime check to prevent throwing. - Added _log.info() when a reinitialization attempt occurs. - Exposed isInitialized as a public read-only getter for improved observability. - Avoids throwing errors in release mode by gracefully handling repeated initialization.
1 parent fb58bcd commit cc058e7

File tree

1 file changed

+12
-8
lines changed

1 file changed

+12
-8
lines changed

packages/supabase_flutter/lib/src/supabase.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Supabase with WidgetsBindingObserver {
4242
/// Call [Supabase.initialize] to initialize it.
4343
static Supabase get instance {
4444
assert(
45-
_instance._initialized,
45+
_instance._isInitialized,
4646
'You must initialize the supabase instance before calling Supabase.instance',
4747
);
4848
return _instance;
@@ -87,10 +87,11 @@ class Supabase with WidgetsBindingObserver {
8787
Future<String?> Function()? accessToken,
8888
bool? debug,
8989
}) async {
90-
assert(
91-
!_instance._initialized,
92-
'This instance is already initialized',
93-
);
90+
if (_instance._isInitialized) {
91+
_log.info('Supabase is already initialized. Skipping reinitialization.');
92+
return _instance;
93+
}
94+
9495
_instance._debugEnable = debug ?? kDebugMode;
9596

9697
if (_instance._debugEnable) {
@@ -152,7 +153,10 @@ class Supabase with WidgetsBindingObserver {
152153

153154
static WidgetsBinding? get _widgetsBindingInstance => WidgetsBinding.instance;
154155

155-
bool _initialized = false;
156+
bool _isInitialized = false;
157+
158+
/// Whether the Supabase instance has been initialized. Useful for debugging.
159+
bool get isInitialized => _isInitialized;
156160

157161
/// The supabase client for this instance
158162
///
@@ -177,7 +181,7 @@ class Supabase with WidgetsBindingObserver {
177181
client.dispose();
178182
_instance._supabaseAuth?.dispose();
179183
_widgetsBindingInstance?.removeObserver(this);
180-
_initialized = false;
184+
_isInitialized = false;
181185
}
182186

183187
void _init(
@@ -214,7 +218,7 @@ class Supabase with WidgetsBindingObserver {
214218
markClientToDispose(client);
215219
}
216220
_widgetsBindingInstance?.addObserver(this);
217-
_initialized = true;
221+
_isInitialized = true;
218222
}
219223

220224
@override

0 commit comments

Comments
 (0)